I'm a relative newbie to Objective-C (only studied Arron Hillegras's book) and am confused by the following snippit of code I've found in one of Apple's code examples, in particular what does the -> mean?
-(id) initWithNumbers:(NSArray *)numbers
{
self = [super init];
if (self != nil)
{
self->_numbers = [numbers copy];
}
return self;
}
In the header file _numbers is declared as
NSNumber * _number;
( the underscore has some significance from what I recall reading somewhere but that too eludes me at the moment.
Thanks Robin
The -> is called the arrow operator. It is formed by using the minus sign followed by a greater than sign. Simply saying: To access members of a structure, use the dot operator. To access members of a structure through a pointer, use the arrow operator. cpp_operators.htm.
The -> (arrow) operator is used to access class, structure or union members using a pointer. A postfix expression, followed by an -> (arrow) operator, followed by a possibly qualified identifier or a pseudo-destructor name, designates a member of the object to which the pointer points.
So for a.b, a will always be an actual object (or a reference to an object) of a class. a →b is essentially a shorthand notation for (*a). b, ie, if a is a pointer to an object, then a→b is accessing the property b of the object that points to.
->
is a normal C operator for accessing the members of a pointer to a struct; the .
operator is for accessing members of a struct. Thus:
a->b
is translated to
(*a).b
Since Objective-C objects are pointers to structs underneath it all, this works for accessing instance variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With