dealloc in Swift

前端 未结 6 1006
猫巷女王i
猫巷女王i 2020-12-23 13:19

I would like to perform some cleanup at the end of a view controller\'s life, namely to remove an NSNotificationCenter notification. Implementing dealloc<

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 13:24

    removing observer is required before deallocation otherwise crash would happen. It can be done using

    deinit {
        // perform the deinitialization
        print("deinit")
    
        removeObserver(self, forKeyPath: kSelectedViewControllerKey, context: nil)
        removeObserver(self, forKeyPath: kSelectedIndexKey, context: nil)
    
    }
    

提交回复
热议问题