NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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 Admin

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 NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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.