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 Swift 2.0 Enumerations and Optionals Introduction to Enumerations Enums and Objects

Andrei K
Andrei K
7,601 Points

Attempting to complete the enum challenge task

Hi, I'm trying to complete the enum challenge task. I'm a bit lost, I don't see how I can add to or subtract from the initialized values in the Point() function. Any help?

classes.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) {
        case Direction.Up {
            self.location = Point(y = y+1)
        }
        case Direction.Down {
            self.location = Point(y = y-1)
        }
        case Direction.Left {
            self.location = Point(x = x-1)
        }
        case Direction.Right {
            self.location = Point(x = x+1)
        }
    }
}

1 Answer

Nathan Tallack
Nathan Tallack
22,160 Points

Your logic was good. Just a few syntax errors. I've corrected them below and given comments on each of them.

In addition I've given you some shortcuts on your syntax and commented them too. :)

    func move(direction: Direction) {
        // We need to switch on the parameter we pass in.
        switch direction {  // The code block contains all the cases inside the braces.
            case .Up:  // Each case separates what it is from what it does with a :
                self.location.y += 1  // We reference the location object property for y like this.
            case .Down:  // We can leave out the Direction and start with . for the known type.
                self.location.y -= 1
            case .Left:
                self.location.x -= 1
            case .Right:
                self.location.x += 1
        }
    }

Keep up the good work!!!