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

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.