Is there any difference to use getters of sythesize properties or direct access to object? [duplicate]
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
.
精彩评论