我应该在 ARC 的 init 方法中引用 self.property 吗?

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

本文介绍了我应该在 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;

在 .m 文件中,如果我使用 ARC,我应该使用 ivar 还是 init 方法中的属性?

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 会触发其他副作用,例如 Key-Value Observing (KVO) 通知,或者您的类实现(显式)或子类覆盖 setWhatever:——这可能会将你部分初始化的实例暴露给其他 API(包括它自己的),这些 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 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我应该在 ARC 的 init 方法中引用 self.property 吗?

基础教程推荐