dealloc in Swift

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

    deinit {
        // perform the deinitialization
    }
    

    From the Swift Documentation:

    A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how intializers are written with the init keyword. Deinitializers are only available on class types.

    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.

提交回复
热议问题