dealloc in Swift

前端 未结 6 1011
猫巷女王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:40

    https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Deinitialization.html

    Swift automatically deallocates your instances when they are no longer needed, to free up resources. Swift handles the memory management of instances through automatic reference counting (ARC), as described in Automatic Reference Counting. Typically you don’t need to perform manual clean-up when your instances are deallocated. However, when you are working with your own resources, you might need to perform some additional clean-up yourself. For example, if you create a custom class to open a file and write some data to it, you might need to close the file before the class instance is deallocated.

    Class definitions can have at most one deinitializer per class. The deinitializer does not take any parameters and is written without parentheses:

    deinit {
        // perform the deinitialization
    }
    

提交回复
热议问题