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

Zac Duvall
Zac Duvall
3,662 Points

I cannot "implement" my move method.

It's asking me to implement my "move" method. and I am confused because I thought by coding:

"robot.move(direction: Direction.right)"

that is me implementing the move method. Can someone explain why the code challenge isn't accepting this.

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) {

        switch direction {
        case .up:
            self.location.y += 1
        case .down:
            self.location.y -= 1
        case .left:
            self.location.x -= 1
        case .right:
            self.location.x += 1
        }

    }
}

Robot().move(in: Direction.left)

2 Answers

Full code that works, just did the challenge

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

Remove default: location

First off your switch is exhaustive so it's not needed and secondly you are saying the default case location which is a type of Point(which is nil at the point, which will never be called but could crash the app if it was)

Zac Duvall
Zac Duvall
3,662 Points

I have removed the default altogether. But my code still isn't passing the challenge. It's still asking for me to "implement the move method"

and I simply don't know what else to do.