添加觀察者是否會增加對象的保留計數? 如果是,ARC是否也會處理這個觀察者的移除?如果不是,我應該在哪裏刪除觀察者?是否需要ARC中的NSNotificationCenter removeObserver?
42
A
回答
78
即使使用ARC
,也應明確刪除觀察者。創建dealloc
方法和刪除那裏..
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
如果你看到的方法,你不必在這裏呼籲[super dealloc];
,只是沒需要超級的dealloc方法。
更新斯威夫特
你可以,如果你在迅速編寫代碼DEINIT方法去除觀測。
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
相關問題
- 1. 在ARC中,我們是否需要發送removeObserver:顯式地?
- 2. NSNotificationCenter是否需要取消UILocalnotficiation?
- 3. Ember手動addObserver是否需要removeObserver
- 4. Xamarin NSNotificationCenter:何時調用RemoveObserver?
- 5. 使用ARC是否NSMutableArray需要屬性?
- 6. 是dealloc上必需的removeObserver?
- 7. NSNotificationCenter:爲什麼打擾調用removeObserver?
- 8. NSNotificationCenter removeObserver不能正常工作
- 9. ARC是否不需要Objective-C中的屬性?
- 10. 是否需要在ARC中發佈子視圖?
- 11. 如果ARC被禁用,在@autoreleasepool中是否需要autorelease?
- 12. 是否需要使用ARC的UIView屬性的retain屬性?
- 13. addobserver/removeobserver查詢
- 14. NSNotification removeObserver問題
- 15. 現在有ARC是否需要使用保留
- 16. 如果我有ARC,我是否需要發佈SystemSoundID?
- 17. 使用NSNotificationCenter時ARC無法正常工作
- 18. 當*完全*是否需要在ARC下複製Objective-C中的塊?
- 19. FlashDevelop中是否需要boildercode?
- 20. IIS中是否需要AppDomain?
- 21. AudioStreamBasicDescription中是否需要mBytesPerFrame?
- 22. 是否支持ARC?
- 23. 是否需要LDT?
- 24. 是否需要AudioServicesDisposeSystemSoundID?
- 25. 是否需要my_require_once?
- 26. 是否需要user_photos?
- 27. UI_APPEARANCE_SELECTOR是否需要?
- 28. 是否需要UserController?
- 29. 是否需要glDisableClientState?
- 30. 是否需要main.cpp?
請參閱[此問題](http://stackoverflow.com/q/13911651/730701)。 – Adam 2013-03-27 10:17:25
這與我問幾個月回來的情況相同:) – 2013-04-03 06:01:02
我認爲重新提出這些問題是有效的,因爲我更願意尋找最新的問題答案,以防感知的智慧發生變化。 – amergin 2013-11-27 12:03:14