加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dealloc在Swift中

发布时间:2020-12-14 06:14:28 所属栏目:百科 来源:网络整理
导读:我想在视图控制器的生命结束时执行一些清理,即删除NSNotificationCenter通知。实现dealloc导致Swift编译器错误: Cannot override 'dealloc' which has been marked unavailable 在Swift中对象生命结束时执行一些清理的首选方法是什么? deinit { // perfor
我想在视图控制器的生命结束时执行一些清理,即删除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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读