I'm trying to use an extension on a class to add conformance to a protocol defining an alternate init()
method and am running into a chain of errors that makes me think I'm missing something. Consider the following:
class ClassA {
let str: String
init(str: String) {
self.str = str
}
}
Assume this class is defined somewhere out of my control. I'd like to do the following:
protocol ProtoA {
init(number: Int)
}
extension ClassA : ProtoA {
convenience init(number: Int) {
self.init(str: String(number))
}
}
With this I get the error Initializer requirement 'init(number:)' can only be satisfied by a 'required' initializer in the definition of non-final class 'ClassA'
. Ok, so I add required
. Then I get 'required' initializer must be declared directly in class 'ClassA' (not in an extension)
.
Of course I can work around this by adding a static function to perform the initialization, but I don't understand why adding another initializer via an extension would be a problem in the first place. Any wisdom would be much appreciated