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

When I return Day.Saturday.rawValue - self.rawValue, xCode says that Day does not have a member named rawValue. Why?

enum Day { case Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday init() { self = .Monday }

func daysTillWeekend() -> Int {
    return Day.Saturday.rawValue - self.rawValue
}

}

4 Answers

The enum case can't have a raw value if you don't specify the enum's "raw" type (Int). So just write : Int after enum Day.

enum Day: Int {
    case Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

    func daysTillWeekend() -> Int {
        return Day.Saturday.rawValue - self.rawValue

    }

}
Oliver Pinchbeck
Oliver Pinchbeck
2,732 Points

And check you're on a recent enough version of Xcode. Rawvalue doesn't exist in Xcode 6.0 according to this: https://teamtreehouse.com/forum/rawvalue-nonexistent-in-version-60-fromraw-and-toraw-are-similar

I am using 6.2 and got the same error with the same code. After some sleuthing, I deleted the last line of code

today.rawValue

and just retyped it in, but only with first letters, and using the TAB button to let Swift fill in the rest. For some strange reason that I have yet to uncover... it worked. No more error. No idea why... if I figure it out, I'll repost. :)

you have to specify the type of the raw value which for your case in integer. So instead of just typing enum Day type enum Day: Int