I am trying to design an abstract class in swift, within which a function takes an instance of a child of that class. However, I want to specify the type of the argument as the name of the abstract class, since I don't know which child of the class would be passed. Example code:
protocol Character {
...
}
extension Character {
...
mutating func fight(target: inout Character) {
target.hp -= 10
}
}
struct Warrior: Character {
...
}
struct Mage: Character {
...
}
foo = Warrior()
bar = Mage()
foo.fight(target: bar)
Here in this code I get the error: Inout argument could be set to a value with a type other than 'Mage', use a value declared as type 'Character' instead, meaning the compiler is not able to regocnize that Mage implements Character and therefore is of type character. How do I solve this?