I have this from either the Apple documentation or the swift book
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? or lets say when we print a property of type string
that was part of a specific instance, also from the deinit
() method? If so, then is the deinit()
method the only way to manipulate a property when it is being deinitialized?
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. 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. 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. You don't need to set properties to nil explicitly in deinit
as that happens automatically.
This being said, most classes don't even need deinit
implemented
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.
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).
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.