dealloc在Swift中

前端之家收集整理的这篇文章主要介绍了dealloc在Swift中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在视图控制器的生命结束时执行一些清理,即删除NSNotificationCenter通知。实现dealloc导致Swift编译器错误
Cannot override 'dealloc' which has been marked unavailable

在Swift中对象生命结束时执行一些清理的首选方法是什么?

deinit {
    // perform the deinitialization
}

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.

原文链接:https://www.f2er.com/swift/321568.html

猜你在找的Swift相关文章