本文介绍了Objective-C中的属性和访问器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码是否调用访问器设置函数或直接修改指针 myMember

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];
    }
}

换句话说,正在调用 setMyMember 方法,或直接修改 myMember 的指针。

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.

同样, myMember = [NSArray数组] 相同self.myMember = [NSArray array]

Likewise, is myMember = [NSArray array] identical to self.myMember = [NSArray array]?

推荐答案

没有 self。变量直接修改。使用它,属性设置器被调用(并且由于你使它成为一个 retain 属性,它被设置的新指针将被发送一个 retain

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).

请参阅Apple的文档和。

See Apple's documentation on declaring and accessing properties.

这篇关于Objective-C中的属性和访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:30