class Animal {
class func generate() -> Animal {
return self()
}
}
The compiler complains constructing an object of class type 'Animal' with a metatype value must use a 'required' initializer
I can understand this. If I write a subclass like this:
class SubAnimal: Animal {
let head: Int
init(head: Int) {
self.head = head
super.init()
}
}
It will inherit Animal
's class method generate()
but won't inherit its default initializer init()
. So SmallAnimal.generate()
actually calls SmallAnimal()
, but SmallAnimal
doesn't have an initializer init()
! Of course this is what the compiler want to prevent.
What confuses me is a similar problem.
class someClass {
}
let anotherClass = someClass.self
let anotherObject = anotherClass()
The compiler still complains constructing an object of class type 'Animal' with a metatype value must use a 'required' initializer.
This time, I cannot understand. anotherClass
is a metatype value, but what bad result will be caused?
I know how to solve this problem, adding required init() {}
is the solution. But I really want to know the reason for the second case.