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 Enumerations and Optionals in Swift Introduction to Enumerations Enums and Objects

Gurpreet Bhachu
Gurpreet Bhachu
3,188 Points

Keep getting a compiler error but I think I am doing everything right

This is the function I've created. It ended up being similar to other solutions on the message board. I'm confused why I keep getting compiler errors...

Firstly, I've updated the enum to include Upper cased direction... I don't know where I am going wrong...

Maybe I need to use a self or a return statement but i've tried both these and it keeps telling me I have a compiler error...

test.swift
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 .Left: location.x -=1
            case .Right: location.x +=1
            case .Up: location.y +=1
            case .Down: location.y -=1
        }
    }
}

2 Answers

You shouldn't update existing code unless specifically asked to do so. I changed the enum back to lowercase then added spaces between = and 1 in the switch statement and it passed.

        switch direction {
            case .left: location.x -= 1
            case .right: location.x += 1
            case .up: location.y += 1
            case .down: location.y -= 1
        }
Gurpreet Bhachu
Gurpreet Bhachu
3,188 Points

I did not at first but tried a bunch of things when I could not get it to pass. I tried it again with the whitespace between the 1 and the ='s and it worked... thank you!