-> is the traditional C operator for accessing a member of the structure referenced by a pointer. Since Objective-C objects are usually used as pointers, and the Objective-C class is a structure, you can use -> to access its members, which (as a rule) correspond to instance variables. Please note: if you are trying to access an instance variable outside the class, then the instance variable should be marked as public.
So for example:
SomeClass *obj = β¦; NSLog(@"name = %@", obj->name); obj->name = @"Jim";
refers to the instance variable name declared in SomeClass (or one of its superclasses) corresponding to the obj object.
On the other hand . used (usually) as dot> syntax for getter and setter methods . For example:
SomeClass *obj = β¦; NSLog(@"name = %@", obj.name);
equivalent to using the getter method name :
SomeClass *obj = β¦; NSLog(@"name = %@", [obj name]);
If name is a declared property , you can assign it to the getter method with a different name.
Dot syntax is also used for setter methods. For example:
SomeClass *obj = β¦; obj.name = @"Jim";
equivalent to:
SomeClass *obj = β¦; [obj setName:@"Jim"];
user557219 Jul 09 '11 at 3:12 2011-07-09 03:12
source share