Using the self. accessor


Everything that I have searched seems to indicate that using the self. assessor [[self myObject] setText: @“Some Text” ] self.myObject.text = @“Some Text” is the better/safer way to “call” or access (Not sure of the proper lingo for this?) an object over calling/accessing it it directly using the ivar [myObject setText:@“Some Text”] myObject.text = @“Some Text”. Can someone offer an explanation why it is this way in this book? I realize this might be splitting hairs but I have a “need” to understand the theory of programming and not just the practice of programming. I realize that my example above are “setters” but my question is the same if I was using “getters”.


As an example based upon the current code.

In BNRItemStore I add another variable called oneItem

In BNRItemStore.m I can do this kind of coding and they both seem to work the same way.

[[self oneItem] setValueInDollars:1]; [oneItem setValueInDollars:1];

The below is just conjecture on my part:

I would imagine that based upon the way self works, it may have more of a performance hit compared to directly accessing the variable itself. I can’t remember where I read it in the book, but it mentions something about self causing the program to search for the (selector?) starting from the current class and if it isn’t found there to search for all the classes that it inherits from, although I’m not sure if this is during compile or runtime.

Another scenario that could happen is that oneItem in the above example is never declared in the program in which case the it will try to search in the inherited classes and it might find another match. It would be hard to track down the problem in such a scenario.