0

Possible Duplicate:
Should I always use accessors for instance variables in Objective-C?

Lets suppose my .h file contains

Node *firstNode

Inside the .m file

What is the difference between

[firstNode doSomething]; and

[[self firstNode] doSomething];
Community
  • 1
  • 1
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • Also: http://stackoverflow.com/questions/5164864/ http://stackoverflow.com/questions/4271657/ http://stackoverflow.com/questions/1332389/ http://stackoverflow.com/questions/4088801/ http://stackoverflow.com/questions/3494157/ http://stackoverflow.com/questions/1519638/ http://stackoverflow.com/questions/3753130/ – jscs Sep 19 '11 at 18:12

2 Answers2

3

[firstNode doSomething]; accesses the instance variable directly while [[self firstNode] doSomething] does not.

Which sounds very obvious, but a getter might do all manner of things, and its declaration can cause all manner of interesting things. Marking the accessor retain affects firstNode's reference count, for instance, while Eimantas' answer tells us that a superclass (or subclass!) might change the precise meaning of [self firstNode].

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
1

assuming you have @property declared for firstNode instance variable, the former method does not use [potentially overridden] getter in your class.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • So, assuming @property is declared, `[self firstNode]` serves no purpose? (The `self` part that is) – James Raitsev Sep 19 '11 at 17:36
  • You can't call `[self firstNode]` if there is no property declared unless you have actual method `- (Node *)firstNode` declared and defined. – Eimantas Sep 19 '11 at 17:37