Objective-C 中的属性和访问器
下面的代码是调用访问器“set”函数还是直接修改指针myMember
?
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];
}
}
换句话说,我想知道是否调用了setMyMember
方法,或者是否直接修改了myMember
的指针。
同样,myMember = [NSArray array]
与 self.myMember = [NSArray array]
相同吗?
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]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有
self.
表示法,则直接修改实例变量。通过它,属性设置器被调用(并且由于您将其设置为retain
属性,因此将向其设置的新指针发送一条retain
消息)。请参阅 Apple 关于声明的文档,以及访问属性。
Without the
self.
notation, the instance variable is modified directly. With it, the property setter is called (and since you made it aretain
property, the new pointer that it's being set to will be sent aretain
message).See Apple's documentation on declaring and accessing properties.