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
Julian Coy
6,774 PointsA note about enums and raw values...
Code in question:
enum test: Int {
case first = 1, second = 2, third, fourth
}
test.first.rawValue // 1
test.second.rawValue // 2
test.third.rawValue // 3
test.fourth.rawValue // 4
enum test2: Int {
case first, second = 2, third, fourth
}
test2.first.rawValue // 0
test2.second.rawValue // 2
test2.third.rawValue // 3
test2.fourth.rawValue // 4
enum test3: Int {
case first = 1, second = 2, fourth, third = 3
}
// This enum is broken and will result in an error
// Error: Raw value for enum case is not unique
//
// This is because the raw value of 3 is assigned to
// 'fourth' automatically.
enum test4: String {
case first = "a", second = "b", third, fourth
}
// This enum is broken and will result in an error
// Error: Enum case must declare a raw value when the
// preceding raw value is not an integer
By running these tests, I noted that the enum itself will attempt to assign incrementally increasing values when given a Raw Value of Int from the first assignment statement it sees. Any subsequent assigns must match that formula or will result in an error. Strings, or other types for that matter, require full assignment of raw values for all cases in the enum.
Let me know if I missed anything. I'm trying to figure out all the intricacies of the enum, as I know it can be very powerful.