Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Inheritance Overriding Methods

sergei zh
sergei zh
2,687 Points

crash of playground

This was my initial code: //---------

import UIKit

class Product { let title: String let price: Double

init(title: String, price: Double) {
    self.title = title
    self.price = price
}

func discountedPrice(persentage: Double) -> Double {
    return price - (price * persentage / 100)
}

}

enum Size { case Small, Medium, Large init(){ self = .Small } }

//----

When I've add this at the bottom:

class Clothing: Product { var size = Size()

override func discountedPrice(_ persentage: Double = 10) -> Double {
    return price - (price * persentage / 100)

}

}

system say "Extraneus '_' in parameter: 'persentage' has no keyword argument name

and then I get message "communication with playground service was interrupted"

I've try this on two different computers, same result. What it can be?

4 Answers

Brian Patterson
Brian Patterson
19,588 Points

I would like to know the answer to this. As this must be specific to Swift 2.0. I am getting the same error.

sergei zh
sergei zh
2,687 Points

It is a bug in xcode 7. Download Xcode 6.4 from Apple site

Brian Patterson
Brian Patterson
19,588 Points

I think I will wait for a update and look forward to the new videos that are coming next week. Will proceed to the next video.

Now Xcode 7 is on general release, this still persists.

There must be a change in how overridden methods are declared as I can't get the video's code to work. It doesn't throw an error - it just crashes.

I can work around it but I will do some digging in the documentation to see if how we should be defaulting parameters has changed. Certainly, the underscore upsets the compiler and as soon as you add a default value to the parameter, the crash occurs.

The documentation of Swift 2.1 says :

"If a default value is defined, you can omit that parameter when calling the function."

So that explains the error message, because the underscore is no longer needed.

As for the crashing, I guess we'll have to wait for an update.

Here's another thing that may help you work around this for now.

If you have a default value for the original method AND for the one you're using to override, the crash does not occur. It only occurs if just one of them has a default value.