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

I need help providing an implementation for the move method, someone please walk me through it and explain the steps

the instructions were : 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 Direction.up: location.y =+ 1
    case Direction.down: location.y -= 1
    case Direction.left: location.x =+ 1 
    case Direction.right: location.x -=1
    } 
}

1 Answer

Hi Mackenzie,

You asked this 2 months ago and nobody answered? That's a shame. You've got the concept down; the only problem is a syntax error for the .up and .left cases of your move() method: the arithmetic operator comes before the equal-sign. Everything else looks fine, from a quick glance, so fixing the above should allow you to pass the Challenge.