Look at a simple class like this:
self is different in instance method and class method, you can see it through the auto completion of Xcode. In instance method,
self’s type is
SomeClass while in class method, it’s type is
The compiler complains constructing an object of class type ‘SomeClass’ with a metatype value must use a ‘required’ initializer
How to understand this? If I write a subclass like this:
It will inherit
SomeClass’s class method
generate() but won’t inherit its default initializer
SubClass.generate() actually calls
SubClass doesn’t have an initializer
init()! Of course this is what the compiler want to prevent.
Then let’s move to a similar problem, it really took me some time to understand it.
The compiler still complains constructing an object of class type ‘SomeClass’ with a metatype value must use a ‘required’ initializer.
It’s true that
anotherClass is a metatype value, but what bad result will be caused?
Consider the case where we also have a subclass:
If you store the class type in a variable:
anotherClass is of type
You can later assign this variable a subclass:
This is valid because
SubClass.Type is a
SomeClass.Type. At this point,
anotherClass() would fail if the initializer is not implemented in the subclass. This is what the compiler is protecting against.
So, why does constructing an object of class type
SomeClass with a metatype value must use a ‘required’ initializer?
SubClass is a subclass of
SubClass is still a
SomeClass, it’s legal for
SubClass to construct an object of class type
SomeClass do but there’s no guarantee that
SubClass has implemented the initializer which is used by
SomeClass to do so. And the guarantee is the required initializer.