Customizing deinitialization

We want to know when the instances of the Circle class will be removed from memory; that is, when the objects aren't referenced by any variable and automatic reference count mechanism decides that they have to be removed from memory. Deinitializers are special parameterless class methods that are automatically executed just before the runtime destroys an instance of a given type. Thus, we can use them to add any code we want to run before the instance is destroyed. We cannot call a deinitializer; they are only available for the runtime.

The deinitializer is a special method that uses the deinit keyword in its declaration. The declaration must be parameterless, and it cannot return a value.

The following lines declare ...

Get Swift 3 ObjectOriented Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.