2

Sometimes I do see

self->someData = d;

in Objective-C code. Can someone explain when that is used?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    You can try find answer here: http://stackoverflow.com/questions/9072688/dot-operator-and-arrow-operator-use-in-c-vs-objective-c – Jakub Jun 08 '12 at 10:36
  • ah, if searching for "arrow operator", then easier... on iBooks or Preview on Mac I can't search for `->` – nonopolarity Jun 08 '12 at 10:41

2 Answers2

5

To access a variable of an instance which isn't listed as a property.

max_
  • 24,076
  • 39
  • 122
  • 211
3

This syntax can be used to directly access public instance variables of an object. (It does not work for private variables.) You probably should create a property and use that instead, or use key value coding.

Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39
  • 1
    and is it true that within the class, this notation can access the private and protected instance variables? It seems that some people also use `self->someData` instead of just `someData` to signal to himself that this is an instance variable within any instance methods – nonopolarity Jun 08 '12 at 10:51
  • 1
    It does work for private instance variables. `someOtherObject->privateIVar` probably won't work, but it will always work if the left hand side is `self`. – JeremyP Jun 08 '12 at 12:17