3

Hi I know that when accessing members in C using the arrow notation (->) makes elminates the need for using both star and dot (* .) every time an object needs to be accessed.

But in Objective-C is there any difference between self.myivar and self->myivar? I have noticed that both of them work in my case, where I have an myivar declared as a property.

David Karlsson
  • 9,396
  • 9
  • 58
  • 103
  • possible duplicate of [Difference between self.instanceVar = X and instanceVar = X in Obj-c](http://stackoverflow.com/questions/5484945/difference-between-self-instancevar-x-and-instancevar-x-in-obj-c) – Sulthan Jun 17 '13 at 15:19
  • `self.myivar` is a method call, `self->myivar` is not. – Sulthan Jun 17 '13 at 15:20
  • so self.myivar only works if a setter is declared by user or @property? – David Karlsson Jun 17 '13 at 15:21
  • Not really, settings works if there is a method `-setMyivar:`. Whether it is declared as a `@property` or not doesn't make a difference. Or method `-myivar` when you are reading. – Sulthan Jun 17 '13 at 15:24
  • So it wont work unless there is a setter? – David Karlsson Jun 17 '13 at 15:30
  • possible duplicate of [Difference between self.ivar and ivar?](http://stackoverflow.com/q/4142177/), [Difference between self and normal variable](http://stackoverflow.com/q/536388/), [Properties and accessors](http://stackoverflow.com/q/6085080/), [Ivar property, access via self?](http://stackoverflow.com/q/4088801/), [When to access properties with self](http://stackoverflow.com/q/4271657/), [What is the (style) difference between “self.foo” and “foo” when using synthesized getters?](http://stackoverflow.com/q/3494157/), and [many more](http://stackoverflow.com/search?q=%5Bobjc%5D+self.ivar) – jscs Mar 01 '14 at 20:15

1 Answers1

9

But in Objective-C is there any difference between self.myivar and self->myivar?

Yes, there's a difference. Assuming that foo is a pointer to an object:

foo->bar is equivalent to (*foo).bar where the dot indicates the member access operator to get the instance variable bar.

foo.bar is equivalent to [foo bar]; that is, it sends the message -bar to the object pointed to by foo. That may just return whatever is in foo's bar instance variable, but it may do other things. There may not even be an instance variable named bar. As long as there's a method called -bar, however, foo.bar is valid. There should also be a -setBar: method if you're using foo.bar as the left hand side of an assignment, like: foo.bar = baz;.

Note that although self is a keyword in Objective-C, it always acts as a pointer to an object. There's nothing special about self with respect to accessing properties or instance variables. I've used foo as the name of the object pointer above to demonstrate that property/ivar access works the same way for any object pointer, but you could substitute self for foo above.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 2
    Note that using `foo->bar` quite effectively breaks encapsulation. It also bypasses KVO. Don't do that. – bbum Jun 17 '13 at 16:54