I want to use SWIFT reflection to resolve all implementations of a certain protocol.
The problem I'm trying to solve is that I want to test all present and future implementations of a certain protocol.
The reflection in SWIFT is limited, but after reading up on it, I believe it should be possible. I have found the following articles on reflection in SWIFT, but I am having trouble mapping it in to my specific use case.
https://nshipster.com/mirror/ https://www.swiftbysundell.com/articles/reflection-in-swift/
How can I resolve and iterate over all implementations of a specific protocol?
protocol Animal {
var color: UIColor { get }
var name: String { get }
}
class Cat: Animal {
var color: UIColor = .red
var name: String = "Joshua"
}
class Dog: Animal {
var color: UIColor = .blue
var name: String = "Anna"
}
class Animals {
var animalCollection: [Animal]
init() {
...
}
}
class TestAnimals {
func testAnimalCollection() {
let mirror = Mirror(reflecting: Animals.self)
for child in mirror.children {
print(child.value)
}
}
}
Aucun commentaire:
Enregistrer un commentaire