BLACK FRIDAY: Save 50% on all books and bundles! >>

SOLVED: No output in Playgrounds although no errors are reported. (Challenge from Unwrap.app)

Forums > 100 Days of Swift

@daa  

// This code should create two classes, then print a fact.

import UIKit

class Bird {
    var wingspan: Double? = nil
}
class Eagle: Bird{}

let bird = Bird()

if let eagle = bird as? Eagle {
    if let wingspan = eagle.wingspan {
        print("The eagle's wingspan is \(wingspan).")
    } else {
        print("This eagle has an unknown wingspan.")
    }
}

I assumed Playgrounds will print to the console: "This eagle has an unknown wingspan" . Instead, it shows this:

No output to the console. Only the newly created bird object.

What am I missing here?

Thank you for your help.

   

@twostraws  Site AdminHWS+

You created an instance of Bird, not an instance of Eagle. Trying to typecast a bird as an eagle will not work.

   

@daa  

Thank you Paul.

   

Hello,

In your code eagle is not a bird.

import UIKit

class Bird {
    var wingspan: Double? = nil
}

class Eagle: Bird {

}

let bird = Bird()

print("start")

if let eagle = bird as? Eagle {
    if let wingspan = eagle.wingspan {
        print ("Eagle wingspan \(wingspan)")
    } else {
        print ("Unkno wingspan")
    }
    print ("Eagle")
} else {
    print ("Not an Eagle")
}

Please type your code here, do not use png

s e b

1      

@daa  

@Sebastien-Remy Thank you. That makes it even clearer to me.

P.S.: The .png file was to show Xcode's output

   

Save 50% in my Black Friday sale.

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.