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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.