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 trial

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,734 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