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

Chad Kusuno
Chad Kusuno
6,789 Points

switch direction { case Direction.left: Point(x: -1, y: 0) I'm stuck on this exercise. Having trouble with +1

switch direction { case Direction.left: Point(x: -1, y: 0) case Direction.right: Point(x: 1, y: 0) case Direction.up: Point(x: 0, y: 1) case Direction.down: Point(x: 0, y: -1) }

I'm stuck. Having trouble visualizing the command to make the Point class move in. above is what I have but I know it's not right

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 Direction.left: Point(x: -1, y: 0) 
      case Direction.right: Point(x: 1, y: 0)
      case Direction.up: Point(x: 0, y: 1)
      case Direction.down: Point(x: 0, y: -1)
      }
    }
}

1 Answer

Some hints:

  • Instead of setting a coordinate to 1 or -1 you will want to move relative to the current location by adding or subtracting 1
  • The current location of the robot is stored in variable location
  • Since location is a Point you can access the x and y coordinates as location.x and location.y
Chad Kusuno
Chad Kusuno
6,789 Points

Kris, thank you for the help. I finally got it. The hints were perfect. You rock.