简体   繁体   English

我对实例初始化后如何处理实例内部的属性感到困惑?

[英]Im confused on how can I manipulate the properties inside an instance when it gets deinitialized?

I have this from either the Apple documentation or the swift book 我从Apple文档或快速书中获得了此信息

When an instance gets deinitialized, you still have access to the properties inside the instance and can manipulate them as needed before the instance totally goes away. 实例取消初始化后,您仍然可以访问实例内部的属性,并且可以在实例完全消失之前根据需要对其进行操作。

I'm confused, do they mean when we for example do some mathematical action using the instances property in the deinit() method? 我很困惑,例如,当我们使用deinit()方法中的instances属性执行一些数学deinit()时,它们是什么意思? or lets say when we print a property of type string that was part of a specific instance, also from the deinit () method? 还是说当我们打印来自特定实例一部分的string类型的属性时,也来自deinit ()方法? If so, then is the deinit() method the only way to manipulate a property when it is being deinitialized? 如果是这样,那么在deinit()属性时, deinit()方法是操纵属性的唯一方法吗?

if you have a swift class with aa var you know you have to clean up after because ARC can't free it (eg C memory), you can still do that in deinit. 如果您有一个带有aa var的swift类,那么您知道必须进行清理,因为ARC无法释放它(例如C内存),您仍然可以在deinit中执行此操作。 The pointers stored in the properties are still valid! 存储在属性中的指针仍然有效!

it isn't useful for much more though (ok end observing with the notification center or kvo) BECAUSE there is no guarantee WHEN deist is called. 但是,它没有更多用处(可以通过通知中心或kvo进行最终观察),因为不能保证何时调用deist。 ONLY that it is called before deallocation [whenever that is] 仅在释放之前(无论何时)都被调用

deinit is called right before deallocation (when the retainCount reaches 0), so all your properties are still valid and you can print your string. deinit被释放之前右称为(当retainCount到达0),因此所有的属性仍然有效,就可以打印字符串。 You don't need to set properties to nil explicitly in deinit as that happens automatically. 您无需在deinit中将属性显式设置为nil,因为这会自动发生。

This being said, most classes don't even need deinit implemented 话虽如此,大多数类甚至都不需要实现deinit

Most of the time I used deinit to remove observer that the instance is registered to, post any notifications if needed, and things like that. 大多数时候,我使用deinit删除实例注册到的观察者,并在需要时发布任何通知,诸如此类。

As far as I know, the deinit method gets called just before the instance gets deinitialized, to give you a final opportuninty to do whatever you need to do (cleanup, close a file, terminate a network connection, etc). 据我所知, deinit方法在实例被deinit 之前被调用,从而为您提供了最终的机会来执行您需要做的任何事情(清理,关闭文件,终止网络连接等)。

What the documentation says is that, at the time deinit is called your object has not been deinitialized yet (but will be very soon), so you still can (for the last time) access its properties. 该文档说的是,在调用deinit ,您的对象尚未被初始化(但很快就会被deinit ),因此您仍然可以(最后一次)访问其属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查看 Controller 未按预期取消初始化 - View Controller is not deinitialized when expected UIVIewController 弹出时未取消初始化 - UIVIewController Not Getting Deinitialized When Popping 在Xcode中,如何在调试区域中获取self的实例属性? - In Xcode, how can I get the instance properties of self in the debug area? 我如何操作主表,以便在 Swift 中使用 splitview 时可以向其添加日历 - How can i manipulate an master table so i can add an calender to it when using splitview in Swift 当我使用我的应用程序时如何关闭其他应用程序的推送通知 - How can I turn off push notifications of other apps when Im using my app iOS:如何确定何时绘制UIView? - iOS: How can I decide when UIView gets drawn? 当ActiveReferenceCount> 0时为什么要取消初始化对象 - Why object is being deinitialized when ActiveReferenceCount > 0 取消初始化时,未拥有的引用是否设置为“ nil”? - Are Unowned references set to 'nil' when deinitialized? 如何在subView中操作对象? - How can I manipulate objects in a subView? 考虑到我不能使用属性,如何为类(不是实例)指定委托? - How do I designate a delegate for a class (not an instance) considering I can't use properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM