super init constructor in Swift

i0S Swift Issue

Question or problem in the Swift programming language:

I used to work with Objective-c using the following code in my class NSObject constructor :

- (id)init {
    self = [super init] ;
    return self;
}

How do I use it in Swift?

I tried to do this:

override init() {
    self = super.init()
    return self;
}

I’m getting two errors:

cannot assign value self is immutable
nil is the only return value permitted in this initializer  

How to solve the problem:

Solution 1:

You can’t assign to self in Swift. Just use

super.init()

You also don’t return anything. Constructors are of type void (in C lingo).

Solution 2:

The Swift initialization sequence has a little bit difference from Objective-C,

class BaseClass {
    var value : String
    init () {
        value = "hello"
    }
} 

the subclass below.

class SubClass : BaseClass {
    var subVar : String
    let subInt : Int

    override init() {
        subVar = "world"
        subInt = 2015
        super.init()
        value = "hello world 2015" // optional, change the value of superclass
    }

}

The initialization sequence is:

  1. Initialize subclass’s var or let,
  2. Call super.init(), if the class has a super class,
  3. Change value of superclass, if you wanna do that.

I think your code:

override init() {
    self = super.init()  //just call super.init(), do not assign it to self
    return self;         //it is unnecessary to return self
}

We must remember initialize all of the var or let in the class.

Solution 3:

In Swift, you don’t assign or return self. In addition, you need to call super.init() after custom initialization. I.e., if your Objective-C code looked something like:

- (instancetype)init {
    if (self = [super init]) {
        someProperty = 42;
    }
}

then the equivalent in Swift would be

init() {
    self.someProperty = 42
    super.init()
}

The reason for this is stated in the Apple documentation:


Safety check 1 A designated initializer must ensure that all of the
properties introduced by its class are initialized before it delegates
up to a superclass initializer.
As mentioned above, the memory for an object is only considered fully
initialized once the initial state of all of its stored properties is
known. In order for this rule to be satisfied, a designated
initializer must make sure that all of its own properties are
initialized before it hands off up the chain.

Solution 4:

Apple document, the Swift Programming Language, about initialization. An example in the document.

class Bicycle: Vehicle { override init() { super.init() numberOfWheels = 2 } } 

Hope this helps!