2

While reading some tutorials i found for the first time the "structure operator", I read that it's used to refer of an instace variable.

-(void)setNumber:(int)n{
   self.number = n;
}

-(void)setNumber:(int)n{
   self->number = n;
}

My doubt is: what it doing exactely? When do i use it? Why should i prefer it to the dot notation?

Tim Doll
  • 85
  • 8
  • 1
    http://stackoverflow.com/questions/9072688/dot-operator-and-arrow-operator-use-in-c-vs-objective-c – Nemanja Boric Sep 16 '13 at 13:53
  • Keep in mind that Objective-C is built on top of C, and all the C syntax "shows through". Basically, the C semantics for `->` was carried over into Objective-C unchanged, while additional semantics were overloaded onto `.`, vs what that token means in C. (And you need to have at least a basic understanding of C before you attempt to learn/use Objective-C.) – Hot Licks Sep 16 '13 at 15:09

2 Answers2

4

Dot syntax in Objective-C is just syntactic sugar which is replaced by the compiler with a method invocation. -> dereferences the pointer and accesses the member directly, with no method invocation.

In an Objective-C method call, you don't need to say self->foo, if your class has an instance variable named foo the compiler will infer the self-> portion of it. Because of this, while it's not completely unheard of, uses of -> with an Objective-C object are comparatively rare.

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • Why didn't they just remove this kind of syntax? it can only cause trubles – Tim Doll Sep 16 '13 at 13:58
  • 2
    There are still legitimate uses (direct access of instance variables of non-self instances -- useful when implementing NSCopying, for instance), not to mention that `->` still has plenty of legitimate uses in C, of which Objective-C is a strict superset. – ipmcc Sep 16 '13 at 14:02
0

The arrow -> operator accesses instance variables directly, which you don't want in general, since it violates encapsulation and circumvents accessor methods (that can be a potential source of problems, like KVO not working or some desired side effects not taking place).

Instead, you should almost always use ., because it accesses the properties using getters and setters.

  • Why didn't they just remove this kind of syntax? it can only cause trubles – Tim Doll Sep 16 '13 at 13:59
  • 3
    The OP posted an example of an accessor method implementation. In those it's proper and common to access the ivar directly. – Nikolai Ruhe Sep 16 '13 at 13:59
  • 2
    @TimDoll There are situations where the arrow notation is needed and makes sense. Also, Objective-C, being a strict superset of C, cannot remove C features. – Nikolai Ruhe Sep 16 '13 at 14:00
  • Can you please make me an example of when it would be needed? – Tim Doll Sep 16 '13 at 14:03
  • 1
    @TimDoll See ipmcc's comment about NSCopying. Implementing `isEqual:` or `compare:` are similar cases. – Nikolai Ruhe Sep 16 '13 at 14:05
  • 1
    @NikolaiRuhe the arrow operator isn't necessary even in an accessor method. Inside any instance method, ivars are accessible without referencing them as `self->ivar`. Also, this answer is technically correct, so your downvote is invalid. –  Sep 16 '13 at 14:15
  • 3
    @H2CO3 The arrow operator is perfectly valid to use in an accessor, as everywhere else. – Nikolai Ruhe Sep 16 '13 at 14:39