How can I call a class function from an initializer? I'm looking for something that would work like:
class A {
var name: String
class func getName() -> String {
return "first class"
}
init() {
self.name = Type.getName()
}
}
class B: A {
var bar: String
override class func getName() -> String {
return "second class"
}
init() {
self.bar = "bar"
super.init()
}
}
A().name // "first class"
B().name // "second class"
B().bar // "bar"
(I'm looking at class functions since they can be overwritten and called before the instance is initialized)
=== Edit ===
This question is well answered by: https://stackoverflow.com/a/24711715/2054629
amusingly xcode fails to compile something like:
class A {
var id: String
class func getId() -> String {
return ":)"
}
init(from id: String?) {
let finalId = id ?? type(of: self).getId()
self.id = finalId
}
}
with: 'self' captured by a closure before all members were initialized
while just using let finalId = type(of: self).getId()
works fine.