In Objective-C what is the difference between accessing a variable in a class by using ->
(arrow operator) and .
(dot operator) ? Is ->
used to access directly vs dot (.
) isn't direct?

- 208,748
- 37
- 389
- 560

- 211
- 2
- 3
-
1Similar Question: [Dot (“.”) operator and arrow (“->”) operator use in C vs. Objective-C](http://stackoverflow.com/q/9072688/642706) – Basil Bourque Jan 12 '17 at 00:12
3 Answers
->
is the traditional C operator to access a member of a structure referenced by a pointer. Since Objective-C objects are (usually) used as pointers and an Objective-C class is a structure, you can use ->
to access its members, which (usually) correspond to instance variables. Note that if you’re trying to access an instance variable from outside the class then the instance variable must be marked as public.
So, for example:
SomeClass *obj = …;
NSLog(@"name = %@", obj->name);
obj->name = @"Jim";
accesses the instance variable name
, declared in SomeClass
(or one of its superclasses), corresponding to the object obj
.
On the other hand, .
is (usually) used as the dot syntax for getter and setter methods. For example:
SomeClass *obj = …;
NSLog(@"name = %@", obj.name);
is equivalent to using the getter method name
:
SomeClass *obj = …;
NSLog(@"name = %@", [obj name]);
If name
is a declared property, it’s possible to give its getter method another name.
The dot syntax is also used for setter methods. For example:
SomeClass *obj = …;
obj.name = @"Jim";
is equivalent to:
SomeClass *obj = …;
[obj setName:@"Jim"];
-
8The distinction is more obvious if you name the instance variable differently from the property (e.g. `@synthesize name = ivar_name`). Then you can only access the instance variable with `someObject->ivar_name` and you can only access the property getter with `someObject.name`. – Chuck Jul 09 '11 at 03:51
-
@Bavarious I find this answer confusing at the moment, why is there is so much discussion of what "name" is? Whether name is getter/setter method of instance type. obj, a pointer, has to be deferenced before you can access name, whatever name may be, corrent? Shouldn't your answer simply that obj->name is same as (*obj).name? Or is your answer saying, in part, that Objective C getters/setters do not have to deference the pointer of obj and are simply available using dot notation? Perhaps if ellipses were not used for each declaration of SomeClass *obj = …; I would understand better ... :) – Brian Ogden May 10 '17 at 19:54
-
@Chuck what does "Objective-C class is a structure" mean? What does it mean for a class to be a structure?! Aren't they essentially different? classes are reference types and structs are value types? (sorry I pinged you but Bavarious is no longer using his account) – mfaani Jul 04 '17 at 17:43
The arrow, ->
, is a shorthand for a dot combined with a pointer dereference, these two are the same for some pointer p
:
p->m
(*p).m
The arrow notation is inherited from C and C has it because the structure member accessing operator (.
) binds looser than the pointer dereferencing operator (*
) and no one wants to write (*p).m
all the time nor do they want to change the operator precedence to make people write *(p.m)
to dereference a pointer inside a structure. So, the arrow was added so that you could do both p->m
and *s.p
sensibly without the ugliness of the parentheses.

- 426,620
- 70
- 833
- 800
When you use the arrow operator ptr->member
it's implicitly dereferencing that pointer. It's equivalent to (*ptr).member
. When you send messages to an object pointer, the pointer is implicitly dereferenced as well.

- 48,188
- 17
- 130
- 149