I would like to know what the protocol equivalent is for an initializer in a simple class that only contains initializing functionality and is only intended to be extended in a concrete class.
So probably the easiest is to show the code - I'm looking for the protocol extension equivalent of the following:
import UIKit
class Thing {
var color:UIColor
init(color:UIColor) {
self.color = color
}
}
class NamedThing:Thing {
var name:String
init(name:String,color:UIColor) {
self.name = name
super.init(color:color)
}
}
var namedThing = NamedThing(name: "thing", color: UIColor.blueColor())
I was expecting the code to look something like:
protocol Thing {
var color:UIColor {get set}
}
extension Thing {
init(color:UIColor) {
self.color = color
}
}
class NamedThing:Thing {
var name:String
var color:UIColor
init(name:String,color:UIColor) {
self.name = name
self.init(color:color)
}
}
I've seen solutions suggested in other StackOverflow questions(eg. How to define initializers in a protocol extension?) but I'm not sure they work nor specifically address this problem of additional parameters in the class initializer.