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

I am jacking this all up

I am trying to finish out this code and i started trying to make it happen with a switch statement and im thinking instead i should use an if else? little help without full answer would be appreciated :)! Thanks!!

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
       Direction.up: return y += 1
       Direction.down: return y -= 1
       Direction.right: return x += 1
       Direction.left: return x -= 1

    }
}
let movingDown = move(Direction.down)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First, and foremost, it doesn't matter at all if you use an if/else setup or a switch statement. They are essentially the same thing. That being said, when we have a bunch of if/else statements that are based on one single thing (in this case the direction), it's generally more readable and concise to use a switch statement.

Notice also, that the move function does not return anything. There will be no need for return statements here.

I'm going to give just a tiny bit of code here and say that I would start with switch direction{}.

But what comes between those braces? Well, we've already said there won't be any return statements because the function has no return type. Also, the function takes direction which is of the enum type Direction. So you don't need to say Direction.up, but rather case .up, because we're switching on direction.

What we're looking to do in the different cases is manipulate the location property, which is of type Point. The location has in itself two properties x and y. Makes sense, right?

So in the case that the direction is .up, we want to increase the y by 1.

:bulb: Just going to show one line of code here to get you started off.

case .up: location.y += 1

Hope this helps and that it wasn't too much code. I prefer leaving hints if I can! :sparkles:

Thank you so much for the help! That was exactly what i needed and laid out very well. I need to practice logical thinking more, ill keep at it!