dealloc在Swift中

dealloc在Swift中,第1张

概述我想在视图控制器的生命结束时执行一些清理,即删除NSNotificationCenter通知。实现dealloc导致Swift编译器错误: Cannot override 'dealloc' which has been marked unavailable 在Swift中对象生命结束时执行一些清理的首选方法是什么? deinit { // perform the deinitializa 我想在视图控制器的生命结束时执行一些清理,即删除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.

总结

以上是内存溢出为你收集整理的dealloc在Swift中全部内容,希望文章能够帮你解决dealloc在Swift中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1061746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存