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 trialwilliamho3
9,799 PointsI'm not getting any error's but i dont understand why my code is wrong (enums and objects)
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) {
switch direction{
case .Up: return location.y += 1
case .Down: return location.y -= 1
case .Right: return location.x += 1
case .Left: return location.x -= 1
}
}
4 Answers
Steve Hunter
57,712 PointsHi William,
You don't want to return
the increment; just increment the location. I prefer ++
and --
but you can use +=
like you have:
func move(direction: Direction) {
// Enter your code below
switch direction{
case .Up: location.y++
case .Down: location.y--
case .Right: location.x++
case .Left: location.x--
}
}
I hope that helps.
Steve.
williamho3
9,799 PointsThanks Steve. I tried that and it still is returning an error. I have even copied at pasted exactly your code here and it is still not working. It just says
swift_lint.swift:37:1: error: expected declaration
^
I dont understand what the problem is?
Steve Hunter
57,712 PointsGive me a minute ...
Steve Hunter
57,712 PointsOdd ... works for me ... did you paste to the right place? When I pasted it, I did it wrong first time; that's why I ask.
If I copy my whole func
over that in the challenge, it passes.
williamho3
9,799 Pointsvery odd. Yes i have pasted over the whole func and it is still returning the error. Thank you so much for your help i dont know what i can possibly be doing wrong!
williamho3
9,799 Pointsi fixed it! Missed the last } of the end Thank you so much for your help !
Steve Hunter
57,712 PointsWell done! :-)
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsP.S. You solved another problem - I've just replied to Mitchell Warmerdam in this post where we had been talking about two different challenges! Oops! Still, the solution was correct.