On "self" and local accessors [duplicate]
开发者_如何学编程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];
[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]
.
assuming you have @property
declared for firstNode
instance variable, the former method does not use [potentially overridden] getter in your class.
精彩评论