Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting a superclass as one of its subclasses

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?

like image 660
Marmelador Avatar asked Dec 10 '25 12:12

Marmelador


2 Answers

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.

like image 62
Vlad Khambir Avatar answered Dec 12 '25 07:12

Vlad Khambir


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")
    }
like image 37
Yannick Avatar answered Dec 12 '25 08:12

Yannick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!