UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

3      

@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.

3      

@daa  

Thank you Paul.

3      

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

4      

@daa  

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

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

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.