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

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.