2012-04-30 30 views
2

在我想要使用init(或initWith)爲屬性賦值時引入了ARC之前,我剛剛使用了retain(作爲不希望在init中使用屬性設置器的結果)。使用ARC在init內設置屬性?

// Pre ARC using retain 
// @property (nonatomic, retain) DataModel *dataModel; 
// @synthesize dataModel = _dataModel; 

- (id)initWithDataModel:(id)newModel { 
    self = [super init]; 
    if(self) { 
     _dataModel = [newModel retain]; 
    } 
    return self; 
} 

弧(同樣不使用二傳手),這是爲newModel分配給數據模型屬性的正確方法是什麼?我的猜測是編譯器(使用ARC)會看到該屬性被定義爲強壯並正確設置了屬性。我很好奇,如果這是正確的?

// Using ARC 
// @property (nonatomic, strong) DataModel *dataModel; 
// @synthesize dataModel = _dataModel; 

- (id)initWithDataModel:(id)newModel { 
    self = [super init]; 
    if(self) { 
     _dataModel = newModel; 
    } 
    return self; 
} 
+1

是的這是正確的 –

+0

謝謝,非常感謝。 – fuzzygoat

+0

可能的重複[我應該引用self.property在ARC方法的init方法?](http://stackoverflow.com/questions/8056188/should-i-refer-to-self-property-in-the-init - 方法 - 用弧) –

回答

0

是的,這是有效的。 它已經是一個綜合屬性。 您可以使用屬性訪問器或合成的直接訪問伊娃名稱。

相關問題