Swift Tutorial - Tutorialspoint

(backadmin) #1

Subclass initializer cannot delegate up to the superclass initializer when overriding a
failable superclass initializer with a non-failable subclass initialize.


A non-failable initializer can never delegate to a failable initializer.


The program given below describes the failable and non-failable initializers.


class Planet {
var name: String

init(name: String) {

self.name = name
}

convenience init() {
self.init(name: "[No Planets]")
}
}
let plName = Planet(name: "Mercury")
println("Planet name is: \(plName.name)")

let noplName = Planet()
println("No Planets like that: \(noplName.name)")

class planets: Planet {
var count: Int

init(name: String, count: Int) {
self.count = count
super.init(name: name)
}

override convenience init(name: String) {
self.init(name: name, count: 1 )
}
}

When we run the above program using playground, we get the following result:

Free download pdf