Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understand deinitialization and inheritance in swift language

Let's say I have two classes: Base class and sub class like this:

class Base{
    var name: String?
    init() {
       name = "The base class"
    }

    deinit {
       println("call Deinitialization in base class")
       name = nil
    }
}

class Sub: Base{
    var subName: String?
    init() {
     super.init()
     subName = "The sub class"
    }

    deinit {
       println("call Deinitialization in sub class")
       subName = nil
       // does it really call super.deinit() ?
       // or assign name = nil ?
    }
}

When the deinitializer of sub class is called, does it call super.deinit() to assign nil to name variable? Or I have to assign by hand in deinitializer of subclass?

like image 928
ductran Avatar asked Jun 03 '14 15:06

ductran


People also ask

What is inheritance in Swift?

Inheritance allows us to create a new class from an existing class. The new class that is created is known as subclass (child or derived class) and the existing class from which the child class is derived is known as superclass (parent or base class). Swift Inheritance Syntax.

How does Swift prevent inheritance?

You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the final modifier before the method, property, or subscript's introducer keyword (such as final var , final func , final class func , and final subscript ).

How do I inherit multiple classes in Swift?

Swift doesn't allow us to declare a class with multiple base classes or superclasses, so there is no support for multiple inheritance of classes. A subclass can inherit just from one class. However, a class can conform to one or more protocols.

What is deinitialization in Swift?

Swift - Deinitialization. Before a class instance needs to be deallocated 'deinitializer' has to be called to deallocate the memory space. The keyword 'deinit' is used to deallocate the memory spaces occupied by the system resources. Deinitialization is available only on class types.

What is swift inheritance?

Swift - Inheritance. The ability to take than more form is defined as Inheritance. Generally a class can inherit methods, properties and functionalities from another class. Classes can be further categorized in to sub class and super class.

What happens when Swift instances are deallocated?

Swift automatically deallocates your instances when they are no longer needed, to free up resources. Swift handles the memory management of instances through automatic reference counting (ARC), as described in Automatic Reference Counting. Typically you don’t need to perform manual cleanup when your instances are deallocated.

How to deallocate memory in Swift?

A deinitialization process which immediately called before a class instance is deallocated. We can start deinitialization with the deinit keyword. deinit is the predefined keyword to deallocate memory in Swift. Inside deinit, we can write our code.


2 Answers

You can optionally have a deinit inside your subclass.

If you do

    let x = Sub()

you'll see that first the deinit called is the one inside Sub() then after, base deinit is called. So yes the super.deinit() is called but after the subclass.

Also the book says (page 286):

You are not allowed to call a deinitializer yourself. Superclass deinitializers are inherited by their subclasses, and the superclass deinitializer is called automatically at the end of a subclass deinitializer implementation. Superclass deinitializers are always called, even if a subclass does not provide its own deinitializer.

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

like image 180
John Riselvato Avatar answered Nov 21 '22 14:11

John Riselvato


Superclass deinitializers are inherited by their subclasses, and the superclass deinitializer is called automatically at the end of a subclass deinitializer implementation. Superclass deinitializers are always called, even if a subclass does not provide its own deinitializer.

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

like image 40
Nate Cook Avatar answered Nov 21 '22 12:11

Nate Cook