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

Swift 2.0 overriding method error can someone please help me downgrade Xcode or get around this error.

I recently updated to Swift 2.0 and immediately got an error causing my program to crash. I double checked my code and even used the code in the teachers notes and it still crashed. Can please someone help me downgrade my Xcode. Here my code:

import UIKit

class Product {

    let title: String
    var price: Double = 0.0

    init(title: String, price: Double) {
        self.title = title
        self.price = price  
    }
    func discountedPrice(_ percentage: Double) -> Double {
        return price - (price * percentage / 100) }    
}

enum Size {

    case Small, Medium, Large

    init() {   
        self = .Medium
    }    
}

class Clothing: Product {

    var size = Size()

    override func discountedPrice(_ percentage: Double = 10.0) -> Double {        
        return price - (price * percentage / 100)        
    }
}

var tshirt = Clothing(title: "T-shirt", price: 500.00)

It would be a lot easier to help you solve your problem if you told us what error it is you're getting now. Downgrading XCode might solve your problem for the moment, if it's a difference between Swift 2.0 and the previous version, but it's a poor solution in the long run. If you're here to learn, you should learn to work with the most recent version of the language. Anything else, and your skills will quickly become obsolete.

18 Answers

I don't think downgrading is an option now - that release is on general distribution, I think.

I have seen the error you're talking about but in my playground it appeared as an orange warning triangle rather than a red issue. Selecting "Fix-it" deleted the underscore and it seems to work fine. Do you get a "Fix-it" option?

Let me try with your code and see what I get.

OK - with your code, I get two orange warning triangles pointing out that the underscore symbols aren't required. Clicking the triangle gets me the "Fix-it" option to delete the underscore. That seems to work. I need to let the latest documentation install before I can check all that as it seems Xcode can't do that and compile a playground at the same time!

You downloaded a beta version of Xcode. There must be a setting somewhere to enable that? I can't find it, but if you uncheck whatever that option is, the stable release should be installed, I think.

I am getting the error: Extraneous '_' in parameter: 'percentage' has no keyword argument name. It also freaks out and crashes when I delete the _.

DO you think the stable version would work if i just downloaded a new version of Xcode over the beta?

Certainly worth a shot. Get rid of the Beta first, or just untick whatever option allowed you to get hold of it, then download a 'standard' Xcode from the Store. I upgraded to Xcode 7.0.1 (7A1001) today - so that's the current stable release. What's yours?

It turns out I have 7.0.1.

LOL! We have a different problem, then!

Let my documentation install and I'll work with your code. As I said before, Xcode seems incapable of doing both at once and it won't let me continue without the upgraded docs. I will then see what I can do about your problem and post back.

Steve.

I'm still trying to figure this out - have you got back up and running yet?

The Product class work fine. Clothing doesn't - it causes the Playground to crash with no obvious errors. The Size enum works fine.

If you remove the override func method completely, Clothing works fine.

I can't figure out what's wrong with that method? Removing the overridden part, the = 10.0 means it works fine too. but adding that in stalls the Playground. I have no idea why!

I modified your func inside Clothing to read:

func discountedPrice() -> Double {
  let percentage: Double = 10.0
  return price - (price * perentage / 100)
} 

That performs the same as your intended code, but that's hardly the point here.

Steve.

I haven't been looking too closely or entered the code into my own playground, but I noticed that you've introduced a typo with this modification. You left out the c in 'percentage' on the return statement, but not the original constant declaration, so that will introduce a new error (assuming this part of the code is actually being run, and not crashing before it gets there.) If it's not introducing a new error, that may also tell you something -- that this function probably is not ever being called.

Also, dang, that's an expensive T-shirt! :-p

That's a good spot - and probably just what I did, rather than the subject of this thread. Thanks for pointing that out - I will sort that once the Mac has installed El Capitan which is it currently doing.

I still get no errors - I worry my Xcode install is knackered!

I'll post back once I've corrected that silly error.

Steve.

Did you notice the T-Shirt error I wonder how that was caused.

No I didn't - what's that? I either got a crashing playground or working code - no errors or explanations!

There is an area with the variable T-Shirt and even when I delete it, it still doesn't work. The t-shirt error is also a yellow triangle.

My Mac has finally finished downloading updates and has now settled down to just working normally.

The code with my amended method is all working fine - the odd typo is't there so I've no idea how that crept into the forum post; apologies for that.

I reverted to your method, the overridden one, but without the = 10.0 in the method skeleton. That works fine. Adding the = 10.0 causes everything to crash - again, no errors, just a playground that crashes.

I must assume that values can't be assigned in this way any more, although I would have expected the issue to be handled more gracefully than to just crash!

Steve.

When i switch it back to overriding it says method does not override anything from superclass

Then, you've not overridden an identical method skeleton. If you change the parameters etc. then that's not overriding. I guess that's overloading - you need to override an identical method.

super.swift
func discountedPrice(percentage: Double) -> Double {}
notoverride.swift
func discountedPrice() -> Double {}

Can you help me fix the error with T-Shirt. It just says expected declaration without a fix-it

var tshirt = Clothing(title: "Vintage", price: 49.99)

What code are you using? Expected declaration means it doesn't now what Clothing is, perhaps? Show me the code and I'll see if I can replicate the error and get round it.

Steve.

All variables have stopped working and the fix-it is to put a semicolon after them and that just causes the expected declaration error to come up.

Weird! Post all your code ...

class Product {
    let title: String
    var price: Double = 0.0

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

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

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

class Clothing: Product {
    var size = Size()
override func discountedPrice(percentage: Double = 10.0) -> Double  {
        return super.discountedPrice(percentage)
}

var tshirt = Clothing(title: "Vintage", price: 49.99)
tshirt.price
tshirt.discountedPrice()
tshirt.size
    let quadcopter = Product(title: "Quadcopter", price: 499.99)

It is still the override that isn't working properly. I like the call to super - that makes sense. But I don't think you need to assign the value in the method header.

Maybe amend that to be in the body, as below, and that'll work:

class Clothing: Product {
    var size = Size()
    override func discountedPrice(percentage: Double) -> Double  {
        return super.discountedPrice(10.0)
    }
}

That doesn't seem to fix it

What are you seeing - it works here?

Steve.

This is copied straight out of Xcode and it is working just fine ...

import UIKit

class Product {

    let title: String
    var price: Double = 0.0

    init(title: String, price: Double) {
        self.title = title
        self.price = price
    }
    func discountedPrice(percentage: Double) -> Double {
        return price - (price * percentage / 100)
    }
}

enum Size {

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

class Clothing: Product {

    var size = Size()

    override func discountedPrice(percentage: Double) -> Double {
        return super.discountedPrice(10.0)
    }
}

var tshirt = Clothing(title: "T-shirt", price: 500.00)

tshirt.title
tshirt.size = .Large
tshirt.size

var aProduct = Product(title: "Something", price: 500.00)

Can I see the code you are using and I am getting the same expected declaration error

Just posted it. ;-)

Let me know if that works for you. The playground takes a little while to get going on my Mac, but it is an old one.

Aha in my code there was a missing bracket for the second class it works now

\o/ woohoo! That took a while!! :-D

Ya think