Search code examples
iosswiftinstancedeallocdeinit

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

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?


Solution

  • 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.