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 a deinitializer within the body of the Circle
class:
deinit { print("I'm destroying the Circle instance with a radius value of \(radius).") }
The following lines show the new complete code for the Circle
class. The code file...