2014-02-24 45 views
1

我必須對屬性屬性較弱的setter方法進行一些操作。但是,我不知道該怎麼做。有人能幫我解決嗎?如何在iOS編程中用弱屬性定義setter方法?

+0

[Objective C中爲弱屬性創建自定義setter的正確方法是什麼?](http://stackoverflow.com/questions/15607404/what-is-the-correct-way-to -create-a-custom-setter-for-a-weak-property-in-objecti) –

回答

3

假設你聲明在頭這樣的弱屬性:

@property (nonatomic, weak) Bar *foo; 

你可以簡單地實現setter方法是這樣的:

- (void)setFoo:(Bar *)foo { 
    _foo = foo; 
    // do your custom things here 
} 

其中_foo是自動生成的實例變量你屬性。

+1

你是否測試了這個代碼,指針的自動調零仍然有效? – NAlexN

相關問題