I would like to perform some cleanup at the end of a view controller's life, namely to remove an NSNotificationCenter
notification. Implementing dealloc
results in a Swift compiler error:
Cannot override 'dealloc' which has been marked unavailable
What is the preferred way to perform some cleanup at the end of an object's life in Swift?
From the Swift Documentation:
is the correct answer for Swift "dealloc".
However, it is good to point out new in iOS 9 that NSNotificationCenter no longer needs to be cleaned up!
https://developer.apple.com/library/content/releasenotes/Foundation/RN-FoundationOlderNotes/index.html#X10_11Notes
but note the points below about strong references, so you may have to worry about cleanup anyway...?
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Deinitialization.html
removing observer is required before deallocation otherwise crash would happen. It can be done using
Be careful when calling a method in other class from deinit it will probably end up in crash