Swift override static method compile error
In class B
, the method list
is a separate method from list
in class A
. They just share the same name, that's all.
The parameters of the two list
methods are actually different:
// A.list
static func list(completion: (_ result:[A]?) -> Void) {
// B.list
static func list(completion: (_ result:[B]?) -> Void) {
A.list
takes an argument of type (_ result: [A]?) -> Void
while B.list
takes a (_ result: [B]?) -> Void
. The array type in the closure type's parameter list is different!
So you're not overridding anything, you're just overloading.
Note:
static
methods can never be overridden! If you want to override a method, use class
instead of static
.
class A {
class func get(completion: (_ result:A?) -> Void) {
completion (nil)
}
}
class B: A {
override class func get(completion: (_ result:B?) -> Void) {
completion (nil)
}
}
In short, as per rule, static methods
can't be overridden.