I have a static function I want to call on a class conforming to a protocol.
protocol P {
static func f();
}
class C: P {
static func f() {}
}
Is there a way to store C.self
as a variable with a type that conforms to the protocol? Below does not compile, but it's what I'm ideally trying to do:
let a: AnyClass<P> = C.self;
a.f();
The type of the object you are trying to store with C.self
is C.Type
.
The Type C
conforms to the protocol P
If you want to store your object by ensuring it conforms to P
, use P.Type
as the type.
Example:
let myObject: P.Type = C.self;
myObject.f();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With