Properties and accessors in Objective-C
Does the following code call an accessor "set" function or does it modify the pointer myMember
directly?
aClass.h
@interface MyClass : NSObject {
NSArray *myMember;
}
@property (nonatomic, retain) NSArray *myMember;
aClass.c
@implementation GameplayScene
@synthesize myMember;
- (id) init {
if ( (self = [super init]) )
{
myMember = [NSArray array];
}
}
In other words, I would like to know if the method setMyMember
is being called, or if the pointer of myMember
is being modified directly.
Likewise, is myMember = [NSArray array]
identical to self.myMember = [NSArray array]
?
Solution 1:
Without the self.
notation, the instance variable is modified directly. With it, the property setter is called (and since you made it a retain
property, the new pointer that it's being set to will be sent a retain
message).
See Apple's documentation on declaring and accessing properties.