[英]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.