16

I've seen this operator pop up quite a few times in example code in "Learn Objective C on the Mac."

I believe it's an operator in the C language which Objective C inherits. I tried Googling and searching Stack Overflow and oddly nothing came up.

Does it have an English name?

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
aaronium112
  • 2,992
  • 4
  • 35
  • 50

8 Answers8

12

It has to do with structures.

When we have a struct available locally on the stack, we access its members with the . operator. For example:

CGPoint p = CGPointMake(42,42);
NSLog(@"%f", p.x);

However, if we instead have a pointer to a structure, we have to use the -> operator:

CGPoint *p = malloc(1*sizeof(CGPoint));
p->x = 42.0f;
NSLog(@"%f", p->x);
free(p);
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
10

-> is not specific to Objective-C. It's a C operator.

Now that's cleared, it's the member access operator, equivalent to a pointer dereference and then using the dot operator on the result.

Say you had a struct like this:

typedef struct Person {
   char *name;
} Person;

Person *jacob = malloc(1*sizeof(Person));

So this statement:

jacob->name = "Jacob";

Is equivalent to this statement:

(*jacob).name = "Jacob";

Of course, don't forget the free:

free(jacob); 
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
9

In C

a->b

is a shortcut for

(*a).b

which is for dereferencing of members of a struct that is pointed to.

This is useful, because of . binds stronger than the dereferencing operator * . So by using -> you avoid having to use these ugly parentheses.

Nubok
  • 3,502
  • 7
  • 27
  • 47
5

It's a member selection (or access) equivalent to a pointer de-reference (as pointed out in comments)

a->member is equivalent to (*a).member in C/C++

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • As a corollary to the above, it's the same thing as it is in C. – jer Dec 20 '10 at 00:47
  • I think that the downvote is because you said that it's a pointer de-reference, while actually the pointer dereference operator is the usual `*`; the C99 standard calls it "member access operator" as the dot (`.`) (§6.6.9). I'd just say that it's a shorthand for the `(*a).Member` notation. – Matteo Italia Dec 20 '10 at 00:50
  • (Changed `Property` to `member`, since `Property` has a different meaning in Objective-C) – Dave DeLong Dec 20 '10 at 00:51
4

The same thing that it means in C. It can be used to access the instance variables of objects directly, but generally this is not the best practice. The dot notation you're referring to is a property, not the usual C dot notation.

Community
  • 1
  • 1
jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 2
    It might be interesting to note that `self->bla` (or plain `bla`) accesses the ivar directly, while `self.bla` accesses it through the property accessors. – Rudy Velthuis Aug 20 '11 at 07:30
  • Yes, that's kind of implied in my answer. In particular, you can't use `self.bla` to access an instance variable because `self` is a pointer to the object in memory. That's why you have to use `->`. And that's why there's no conflict between C dot notation and Obj-C properties :) – jtbandes Aug 20 '11 at 07:31
2

a->b is equivalent to (*a).b, and designates member b of the object pointed to by a.

In the C standard, it is called the "structure/union pointer operator," which isn't really the catchiest name.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
2

It's the "indirect member access operator". It's a C operator, which both Objective-C and C++ inherited.

This

a->b

is equivalent to:

(*a).b

but is less typing. (The parens are necessary in the expanded form due to precedence of * and ..)

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
2

It's all been said, it is a shortcut for accessing members of a struct pointer, but just a note to add that you can access ivars using the same syntax due to the way the Objective-C runtime works:

@interface Foo : NSObject {
   NSString *something;
}

/* ... SNIP ... */

NSLog(@"something = %@", foo->something); // Where foo is an instance of the Foo class

This is handy when you need to access ivars that aren't exposed by methods; specifically when you need to implement copyWithZone: on your objects.

d11wtq
  • 34,788
  • 19
  • 120
  • 195
  • thats ok but can you tell me what does this mean. loginViewController->mPortSIPSDK = mPortSIPSDK; first mportSIPSDK is instance of PORTSIPSDK which is declared in loginviewcontroller.Second is ie(=mPortSIPSDK)is declared in aoodelegate. – Suraj K Thomas Dec 09 '13 at 06:20