NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.