开发者

Is there any difference to use getters of sythesize properties or direct access to object? [duplicate]

This qu开发者_如何学编程estion already has answers here: Closed 11 years ago.

Possible Duplicate:

iVar property, access via self?

I understand that it's to use setters for instance variables, but are ther any reasons to use getters istead of direct access?

.
.
NSArray *instArray;
.
.
@property (nonatomic, retain)NSArray *instArray;
.
.
@synthesize instArray;
.
.
//are the following lines equal?
NSArray *array = [NSArray arrayWithArray:self.instArray];

NSArray *array = [NSArray arrayWithArray:instArray];


For purely synthesized getters, there is no difference, unless you want atomic access (atomic properties surround the code with a lock).

For properties that have getters with extra code, there is a difference. Sometimes, it makes sense to avoid the extra code, so you use the ivar (say, xyz) directly. Otherwise,if you want the extra code to be invoked, you use the property, using self.xyz.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜