久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我應該在 ARC 的 init 方法中引用 self.property 嗎?

Should I refer to self.property in the init method with ARC?(我應該在 ARC 的 init 方法中引用 self.property 嗎?)
本文介紹了我應該在 ARC 的 init 方法中引用 self.property 嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

一個簡單的問題.

如果我有一個屬性和一個用相同名稱聲明的 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 嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動運行/壓力測試 iOS 應用程序以找出罕見 UI 錯誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態和動態單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級到 Xcode 4.5 后,iOS 5 SDK 消失了)
Having trouble creating UIImage from CIImage in iOS5(在 iOS5 中從 CIImage 創建 UIImage 時遇到問題)
主站蜘蛛池模板: 99视频免费在线观看 | 亚洲一区在线播放 | 久久精品小视频 | 亚洲热在线视频 | 亚洲国产精品一区二区久久 | 国产成人精品亚洲日本在线观看 | 福利片在线看 | 欧美日韩一区二区在线 | 国产亚洲精品久久久久久豆腐 | 欧美极品在线 | 天堂一区二区三区 | 在线观看视频亚洲 | 玖玖国产 | 国产精品久久久久久模特 | 九九久久国产 | 国产情侣久久 | 免费a级毛片在线播放 | 国产精品一区二区在线免费观看 | 亚洲天堂久久 | 色综合久 | 亚洲精品永久免费 | 精品国产18久久久久久二百 | 操操日 | 一区二区三区日本 | 一区二区三区精品视频 | 国产成人精品久久二区二区 | 国产一区二区三区久久久久久久久 | 午夜影院在线观看免费 | 欧美成人a∨高清免费观看 色999日韩 | 99热都是精品 | 激情欧美日韩一区二区 | 99热精品在线观看 | 欧美视频| 中文字幕免费在线观看 | 99久久精品视频免费 | 成人在线视频网 | 91精品一区 | 中国一级毛片免费 | 精品久久久久久亚洲综合网站 | 成人免费看片又大又黄 | 亚洲视频在线观看 |