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 Enums and Structs Enums and their Methods Initializer

Code that works in xCode is not working in the treehouse editor

Hi I wrote the following code to complete the challenge but the web editor does not seem to accept it as the correct answer and I do not quite seem to be able to figure out the issue with the code since it works perfectly fine in a xCode playground (a default instance provides the correct raw value)

Any help would be appreciated.

enum.swift
enum Coin: Int {
    case Penny = 1, Nickel = 5, Dime = 10, Quarter = 25 

    init() {
    self = Quarter
    }

    func isGreater(currentValue: Coin, newValue: Coin) -> Bool {
        return currentValue.rawValue > newValue.rawValue
    }
}

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi mohitbhatia,

The only issue I see with your code is you don't have a dot before Quarter which is required otherwise the swift compiler will assume it's a variable.

Thanks a lot for the answer. It got the code working just fine for the challenge. The only remaining question I have is that the code above functions in an xCode playground just fine without the additional dot. I verified this by creating a new variable using the enum it checking the raw value of the default case and I got 25 so I am not entirely sure as to what is going on here?

```Swift <p> enum Coin: Int { case Penny = 1, Nickel = 5, Dime = 10, Quarter = 25

init() {
    self = Quarter
}
func isGreater(currentValue: Coin, newValue: Coin) -> Bool {
    return currentValue.rawValue > newValue.rawValue
}

}

let coin = Coin() coin.rawValue

Chris Shaw
Chris Shaw
26,676 Points

I believe this is due to a recent change in Xcode 6.1, because the constructor lives within a specified context i.e. Coin it infers the base type rather than assuming it's a variable.

Your explanation does makes perfect sense. Thanks a lot Chris I appreciate the help.