Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Enumerations and Optionals Introduction to Enumerations Enums and Objects

I'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

Hi 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.

P.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.

Thanks 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?

Give me a minute ...

Odd ... 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.

very 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!

i fixed it! Missed the last } of the end Thank you so much for your help !

Well done! :-)