i have a base class with a private memberwise init, and a public convenience init:
public
class Base
{
let a:[Int]
let b:[Int]
let c:[Int]
private
init(a:[Int], b:[Int], c:[Int])
{
self.a = a
self.b = b
self.c = c
}
public convenience required
init(x:[Int])
{
...
self.init(a: a, b: b, c: c)
}
public
func f()
{
...
}
}
now i want to define subclasses that adds no stored properties but overrides some of the virtual class members. however, this requires duplicating the implementation of init(x:)
, since a subclass init cannot call a superclass convenience init.
public final
class Subclass:Base
{
public required
init(x:[Int])
{
...
super.init(a: a, b: b, c: c)
}
public override
func f()
{
...
}
}
one way to to satisfy the compiler error is to make the superclass init(x:)
a designated initializer. however a designated init cannot call another designated init, which means it cannot delegate to the memberwise init, instead it must assign to the instance properties from within the body of the complex init.
are there any better ways to work around this restriction?