Few blocks of code:
This works:
import Foundation
class Class1 {
init(param1: String?) {
print("hello")
}
convenience init() {
self.init(param1: nil)
}
}
class SubClass1: Class1 {
}
let obj = SubClass1()
In the above code, the designated initialiser is passed down to SubClass1 because SubClass1 does not provide its own designated initialiser. Also, convenience initialiser is also passed down. Therefore, this works.
import Foundation
class Class1 {
init(param1: String?) {
print("hello")
}
convenience init() {
self.init(param1: nil)
}
}
class SubClass1: Class1 {
override init(param1: String?) {
print("hello 2")
super.init(param1: nil)
}
}
let obj = SubClass1()
This works too because SubClass1 has provided its own designated initialiser now.
import Foundation
class Class1 {
init(param1: String?) {
print("hello")
}
init(param2: String?) {
print("world")
}
convenience init() {
self.init(param1: nil)
}
}
class SubClass1: Class1 {
override init(param1: String?) {
print("hello 2")
super.init(param1: nil)
}
}
let obj = SubClass1()
This gives "error: missing argument for parameter 'param1' in call" for the last line. Here, there is a designated initialiser for SubClass1 and convenience initialiser from parent also calls the same initialiser, then why the error?
This
import Foundation
class Class1 {
init(param1: String?) {
print("hello")
}
required init(param2: String?) {
print("world")
}
convenience init() {
self.init(param1: nil)
}
}
class SubClass1: Class1 {
override init(param1: String?) {
print("hello 2")
super.init(param1: nil)
}
required init(param2: String?) {
print("world")
super.init(param2: nil)
}
}
let obj = SubClass1()
and this works:
import Foundation
class Class1 {
init(param1: String?) {
print("hello")
}
init(param2: String?) {
print("world")
}
convenience init() {
self.init(param1: nil)
}
}
class SubClass1: Class1 {
override init(param1: String?) {
print("hello 2")
super.init(param1: nil)
}
override init(param2: String?) {
print("world")
super.init(param2: nil)
}
}
let obj = SubClass1()
Why does it need all the designated initialisers to be overridden for the one convenience initialiser to work that calls just one of the designated initialisers?