Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -> mean?

Tags:

objective-c

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

like image 209
ManorBoy Avatar asked Dec 22 '10 07:12

ManorBoy


People also ask

What does -> mean in coding?

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.

What does the -> operator do?

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.

What is A -> B in C++?

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.


1 Answers

-> 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.

like image 175
Jonathan Sterling Avatar answered Sep 20 '22 06:09

Jonathan Sterling