Properties and accessors in Objective-C

梦想的初衷 提交于 2020-01-18 05:45:08

问题


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]?


回答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.



来源:https://stackoverflow.com/questions/6085080/properties-and-accessors-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!