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 trialEdward Kelly
3,705 PointsCompiler Error in Challenge (Swift Enums and Structs)
For this challenge, we are asked to create a variable named 'turtleSpeed' and assign it the enum value of Slow. I am unable to complete the challenge due to a compiler error. Here is my code, which I believe is correct and I don't know how else to format it:
enum Speed: Int { case Slow = 10 case Medium = 50 case Fast = 100 }
var turtleSpeed: Int = Speed.Slow
The compiler is complaining that:
swift_lint.swift:9:30: error: 'Speed' is not convertible to 'Int' var turtleSpeed: Int = Speed.Slow
4 Answers
Stone Preston
42,016 Pointswoops. in your question you said:
we are asked to create a variable named 'turtleSpeed' and assign it the enum value of Slow.
however task 2 states: Create a variable called turtleSpeed and assign it the Raw value of the member Slow. so you need to use raw value.
enum Speed: Int {
case Slow = 10
case Medium = 50
case Fast = 100
}
var turtleSpeed = Speed.Slow.rawValue
Stone Preston
42,016 Pointsyou are getting member values and raw values of enums confused. the type declared after the : in the enum declaration is the type of the enums raw value, not the type of the enum itself. The type of an enum member itself is the name of the enum (In this case Speed)
the problem is that you are assigning your turtleSpeed variable a value of Speed.Slow. Speed.Slow has a type of Speed, but your variable has its type declared as Int.
while Speed.Slow's raw value is of type int, the type of an enum member itself is just the name of the enum.
ifyou let swift infer the type:
enum Speed: Int {
case Slow = 10
case Medium = 50
case Fast = 100
}
var turtleSpeed = Speed.Slow //turtleSpeed has an inferred type of Speed
if you wanted to explicitly type your variable you could use:
var turtleSpeed: Speed = Speed.Slow //turtleSpeed has an inferred type of Speed
if you wanted to assign a variable the raw value of Speed.Slow you could use:
var turtleSpeedNumber: Int = Speed.Slow.rawValue
Edward Kelly
3,705 PointsHi Stone;
Thanks so much for the reply. I had tried to let the compiler infer the type before posting this question. But that doesn't work, either. When I try to submit the challenge, I get the error:
Bummer! The variable 'turtleSpeed' was expected to be of type 'Int', but it wasn't.
Is there a limitation in the Challenge tool?
Edward Kelly
3,705 PointsOops! Geez, I feel really humbled.
Thanks so much for the reply!
Ed