开发者

On "self" and local accessors [duplicate]

This question already has answers here: Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜