我应该在ARC的init方法中引用self.property吗? [英] Should I refer to self.property in the init method with ARC?

查看:178
本文介绍了我应该在ARC的init方法中引用self.property吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题。

如果我有一个属性和一个声明同名的ivar:

if I have a property and an ivar declared with the same name:

在.h文件中:

(Reminder*)reminder;
@property(nonatomic,strong)(Reminder*)reminder;

如果我'我应该在init方法中使用ivar或属性使用ARC?

in the .m file, should I use the ivar or the property in the init method if I'm using ARC?

- (id)initWithReminder:(Reminder*)reminder_ {
    self = [super init];
    if (self) {
        reminder = reminder_;
    }
    return self;
}

或者我应该使用该属性来获得自动引用计数的好处这个:

Or should I use the property to get the benefit of the automatic reference counting like this:

- (id)initWithReminder:(Reminder*)reminder_ {
    self = [super init];
    if (self) {
        self.reminder = reminder_;
    }
    return self;
}

我不确定对象初始化中的哪个属性可以访问使用点表示法。

I'm not sure at which point in the object's initialization the properties become accessible with the dot notation.

推荐答案

在部分构造的状态下使用直接访问,无论ARC如何:

Use direct access in partially constructed states, regardless of ARC:

- (id)initWithReminder:(Reminder*)reminder_ {
    self = [super init];
    if (self) {
        reminder = reminder_;
        // OR
        reminder = [reminder_ retain];
    }
    return self;
}

这是因为 self.whatever 将触发其他副作用,例如键值观察(KVO)通知,或者您的类实现(显式)或子类重写 setWhatever: - 和这可能会将部分初始化的实例暴露给其他API(包括它自己的),这正确地假设它们正在处理完全构造的对象。

This is because self.whatever will trigger other side effects, such as Key-Value Observing (KVO) notifications, or maybe your class implements (explicitly) or a subclass overrides setWhatever: -- and that could expose your partially initialized instance to other APIs (including its own), which rightly assume they are dealing with a fully constructed object.

可以手动验证一个类是否能够在部分初始化状态下运行,但这需要大量维护,并且(当然)当其他人想要为您的类创建子类时,这是不切实际或不可能的。这需要大量的时间和维护,并且这样做没有实质性的好处,特别是如果你试图将这种方法作为惯例使用。

You could manually verify that a class is capable of operating in a partially initialized state, but that requires a lot maintenance and is (frankly) impractical or impossible when other people want to subclass your class. It requires a lot of time and maintenance, and there isn't substantiative benefit doing so, especially if you try to use the approach as a convention.

所以统一的方式保证正确性的是在部分构造的状态下使用直接访问,并避免使用访问器。

So the uniform manner which guarantees correctness is to use direct access in partially constructed states, and avoid using the accessors.

注意:我使用部分构造,因为初始化只是一半的图片; -dealloc 有类似的警告。

Note: I am using "partially constructed" because initialization is only half of the picture; -dealloc has similar caveats.

关于为什么要在部分构造的状态下使用直接访问的更多细节( ARC || MRC)可以在这里找到:初始化属性,点符号

Some more detail as to why you should use direct access in partially constructed states (ARC || MRC) can be found here: Initializing a property, dot notation

这篇关于我应该在ARC的init方法中引用self.property吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆