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

why couldn't i use increment and decrement operators on this enum switch statement?

case .Up: return location.y++

gets an error

case .Up: return location.y = location.y+1

does not. why can't i use the increment operator?

2 Answers

Brendan Whiting
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,696 Points

You can use the increment operator. The problem is that you shouldn't be returning it, just modifying the property.

class Robot {
    var location: Point

    init() {
        self.location = Point(x: 0, y: 0)

    func move(direction: Direction) {
        // Enter your code below
        switch direction {
          case .Right: location.x++
          case .Left: location.x--
          case .Up: location.y++
          case .Down: location.y--

Try the following:

        case .Right: location.x += 1
        case .Left: location.x -= 1
        case .Up: location.y += 1
        case .Down: location.y -= 1