How does one list types that conform to a protocol for purely Swift based types (i.e. no @objc
annotation involved)? I was hoping that a reflection API that allows for this would be provided in the Swift standard library.
Just to be doubly sure, I am aware of the more specialised case of Objective-C or @objc annotated Swift classes conforming to a protocol solvable with Objective-C runtime APIs: How to list all classes conforming to protocol in Swift? – what I am after is the same for an arbitrary Swift type that may be a struct, enum or a class.
Here is my feeble failed attempt at using the Mirror API for the purpose:
protocol Derpable { func derp(); } extension Derpable { func derp() { print("Herp derp.") } } enum E: Derpable { } class C: Derpable { } struct S: Derpable { } print(Mirror(reflecting: Derpable.self).children.count) // prints "0\n"
Aucun commentaire:
Enregistrer un commentaire