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 trialhillionaire
22,539 PointsHOW?
how
class Point {
var x: Int
var y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
enum Direction {
case left
case right
case up
case down
}
class Robot {
var location: Point
init() {
self.location = Point(x: 0, y: 0)
}
func move(direction: Direction) {
// Enter your code below
switch(direction) {
case Direction.Up: location.y += 1
case Direction.Down: location.y -= 1
case Direction.Left: location.x -= 1
case Direction.Right: location.x += 1
}
}
}
}
4 Answers
akyya mayberry
3,668 PointsI can't retrieve my old code. But I do not recall the move method initially not requiring external arg name (the underscore). I copied hillionaire's code. I removed the extra parenthesis in his code, added the underscore in the move definition to remove the external name, lowercased the member names in the method body and the rest of his/her code passed as is. Weird.
Daniel Santos
34,969 PointsThe problem is in the case of the switch statement. The cases in the enum Direction are lower case. You are writing it upper case. You are probably getting an undefined error. Here is another way of doing it, remember Swift is a pretty smart language, in my opinion.
switch direction {
case .left: location.x -= 1
case .right: location.x += 1
case .up: location.y += 1
case .down: location.y -= 1
}
Because Swift knows the type of direction
, explicitly writing the type is not necessary.
Hope this helped.
-Dan
akyya mayberry
3,668 PointsDaniel Santos it doesn't work! Must be some kind of bug on Treehouse end. For one the directions is wrong by telling us to refer to it as Direction.Up which is hillinair may have done it that way. After I kept getting compiler errors, I tried it that way as well. Also, your .down is missing an assignment.
akyya mayberry
3,668 PointsI found the other issue. Once you follow Daniels feedback to adjust your switch statement, you also have to make changes to your enum. The problem is how they want us to define the cases in our enum. They are forcing us to use the alternate syntax that was introduced in the video. The syntax where the case statements all are defined together
enum Direction { case left, right, up, down }
Daniel Santos
34,969 PointsThat is very strange, my code worked fine with the enum provided by the challenge. Here is my full solution:
class Point {
var x: Int
var y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
enum Direction {
case left
case right
case up
case down
}
class Robot {
var location: Point
init() {
self.location = Point(x: 0, y: 0)
}
func move(_ direction: Direction) {
// Enter your code below
switch direction {
case .left: location.x -= 1
case .right: location.x += 1
case .up: location.y += 1
case .down: location.y -= 1
}
}
}
Daniel Santos
34,969 PointsDaniel Santos
34,969 PointsThat is weird.