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

need help with this

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:
        return location.y += 1
    case .Down:
        return location.y -= 1
    case .Right:
        return location.x += 1
               case .Left:

        return location.x -= 1
    }

}

}

// posible solution #2 /* switch direction { case .Up: location.y++ case .Down: location.y-- case .Right: location.x++ case .Left: location.x-- } */

// posible solution #3

/* 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 } */

/* switch direction { case .Up: return location.y += 1 case .Down: return location.y -= 1 case .Right: return location.x += 1 default: return location.x -= 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 .Up:
            return location.y += 1
        case .Down:
            return location.y -= 1
        case .Right:
            return location.x += 1
                   case .Left:

            return location.x -= 1
        }

    }
}

// posible solution #2
/*
switch direction {
 case .Up:
 location.y++
 case .Down:
 location.y--
 case .Right:
 location.x++
 case .Left:
 location.x--
 }
*/  


// posible solution #3

/*
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
        }
*/

/*
switch direction {
        case .Up:
            return location.y += 1
        case .Down:
            return location.y -= 1
        case .Right:
            return location.x += 1
        default:
            return location.x -= 1
        }
        */

3 Answers

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

}

In this one I use a switch statement with each case

perfect, thanks.

Beatty Jamieson
Beatty Jamieson
3,660 Points

Thank you. This was hanging me up too. Xcode wasn't rejecting my code, but treehouse kept giving me the error "make sure you are increasing y coordinate by 1", but didn't mention any other coordinates, which didn't make sense to me why it would only reject the one point if they all were written the same way.

switch direction {
        case .left: Point(x: -1, y: 0)
        case .right: Point(x: 1, y: 0)
        case .up: Point(x: 0, y: 1)
        case .down: Point(x: 0, y: -1)
        }