I have a function that takes an instance of type Being. There are two subclasses to Being: Person and Animal. I want the function to recognize which of the two was passed and do something with properties specific to each subclass.
I thought this would work:
func fight(attacker1: Being, attacker2: Being) -> String {
if attacker1 is Person {
let name1 = attacker1.name as! Person //Error: Value of type Being has no member name
} else {
print("Its not a person")
}
But it doesn't. What is the smoothest/shortest way to achieve what I am looking for?
You should create new variable of the needed type and put on your parameter into this variable.
func fight(attacker1: Being, attacker2: Being) -> String {
if let att1 = attacker1 as? Person {
let name1 = att1.name
} else {
print("Its not a person")
}
In this way, if attacker1 is Person, you just save this value into att1 constant and later you can use this constant as instance of the Person type.
While @V.Khambir's code works and probably is the nicer solution, it did not point out the problem of you code. You are trying to cast .name to a Person. .name is not a property of attacker1, that is why you are getting the error. What you wanted to do is cast attacker1 to a Person and then access the name property.
func fight(attacker1: Being, attacker2: Being) -> String {
if attacker1 is Person {
let name1 = (attacker1 as! Person).name
} else {
print("Its not a person")
}
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