1

In the following code, why is myRide.drive() printing a class Car instead of "Driving at 200"?

class Car {
    var topSpeed = 200

    func drive() {
        print("Driving at \(topSpeed)")
    }
}

class Futurecar : Car {
    func fly() {
        print ("Flying")
    }
}


let myRide = Car() // Car
myRide.topSpeed // 200
myRide.drive() // Car

let myNewRide = Futurecar() // Futurecar
myNewRide.topSpeed // 200
myNewRide.drive() // Futurecar
myNewRide.fly() // Futurecar

I understand that the class Futurecar is inheriting from the car class. Thanks!

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • `myRide.drive()` does print "Driving at 200" ... – Apart from that, you did not post your real code (e.g. Futurecar != Futurcar) – Martin R Sep 24 '17 at 08:21
  • based on your code, I got the following logs: "Driving at 200 Driving at 200 Flying", which means that `myRide.drive()` prints "Driving at 200" – Ahmad F Sep 24 '17 at 08:22
  • @Martin R. It was a typo. –  Sep 24 '17 at 08:24
  • @Ahmad F The debug area is showing me Car –  Sep 24 '17 at 08:25
  • Possible duplicate of [How to print to console using swift playground?](https://stackoverflow.com/questions/24003092/how-to-print-to-console-using-swift-playground) – Martin R Sep 24 '17 at 08:27

1 Answers1

0

I suppose you are doing this in a playground.

In the playground, there is an extra panel on the right there right? I think all these outputs you got are all from the right panel.

The right panel does not necessarily display text printed to the console. When you write a variable, the right panel will display the value of that variable. When you write a method call, the right panel will display the return value of the method. If the method does not return a value, it will display the object on which the method is called.

In this case, drive does not return a value. The right panel displays the object on which it is called on - a FutureCar object.

The above explains the output you get. Now let's move on to see how we can see the text printed. In the bottom of the Xcode window, you will see this:

enter image description here

Click on the button with a triangle inside a rectangle. That will show the console window. The output from your print statements will be displayed here!

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Yes. It's the swift playground. Thanks! –  Sep 24 '17 at 08:26
  • @Dani That's what I thought. Did you see the expected output after opening the console? – Sweeper Sep 24 '17 at 08:27
  • Yes! I was confused. Thanks! –  Sep 24 '17 at 08:29
  • @Dani If you think my answer answers your question, please consider accepting it by clicking on that checkmark when the system allows you! – Sweeper Sep 24 '17 at 08:30
  • I can't because I have less than 15 reputations. –  Sep 24 '17 at 08:31
  • @Dani I mean the checkmark, not the arrows. You can do that 16 minutes after you asked the question. – Sweeper Sep 24 '17 at 08:32
  • I am running into a file path error. My account is not allowed to ask question IDK why. for sound in soundFilenames { do { let url = NSURL(fileURLWithPath: NSBundle.mainBundle(). pathForResource(sound, ofType: "mp3)!) let audioPlayer = try AVAudioPlayer(contentsOfURL: url) audioPlayers.append(audioPlayer) } catch { audioPlayers.append(AVAudioPlayer()) } } } –  Sep 27 '17 at 23:36