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

Duong Nguyen
Duong Nguyen
3,292 Points

Please tell me what I did wrong for this enumeration code challenge?

I tried to follow the directions and examples, but it didn't work. Where did I go wrong?

In the editor below you have two objects - classes named Point and Robot. The Robot stores its location as a point instance and contains a move function.

The task of this challenge is to complete the implementation for move. Move takes a parameter of type Direction which is an enumeration listing the possible movement directions.

When you tell the robot to move up (by specifying Direction.Up as the argument), the y coordinate should increase by 1. Similarly moving down means the y coordinate decreases by 1, moving right means the x coordinate increases by 1 and finally left means x decreases by 1.

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) {
        // Enter your code below
        switch direction {
          case .left: return location.x -= 1
          case .right: return location.x += 1
          case .up: return location.y += 1
          case .down: return location.y -= 1
         }
    }
}

3 Answers

Martel Storm
Martel Storm
4,157 Points

Your answer is already correct buddy! Good job

switch direction {
          case .left: return location.x -= 1
          case .right: return location.x += 1
          case .up: return location.y += 1
          case .down: return location.y -= 1
         }
    }
}
Duong Nguyen
Duong Nguyen
3,292 Points

Thank you, may be something was wrong with the system. Now it worked for me ^^

For future people having problems: I had a problem with entering too and I found a bracket missing. If you are doing your challenges in a Playground page, make sure the brackets are copied for the switch statement.