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

doesn't add or subtract the value when calling the function move

So I was doing the code challenge to learn more about enums and I figured out the solution to the problem I'm pretty sure(i haven't checked it yet). but basically I went to test it out in the swift playgrounds and it does not add a value or subtract a value when you call the move function. hopefully my code will better explain it, but basically if i do move(.left) and check the x value of Robot's point after words it is still zero, Im guessing this has something to do with reference types and also class Robot initializes point with hardcoded values. Idk maybe im going somewhere with that idea here is my code oh and i guess it won't let me upload my playground so ill type out what im doing

//new instance of robot let newRobot = Robot() newRobot.location // should return 0,0 if you print() it newRobot.move(.left) // changes the value of locations x from 0 to -1 newRobot.location //should return the new changes of x but still says 0,0

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

1 Answer

You are very close, the reason the x and y values are not updating is because you need to use the compound assignment operator (+=) in the move function so as to effectively (using y value for the up case as an example) say y = y + 1, instead of simply y + 1.

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

Yeah I realized this right after I posted the question thank you for posting the answer though.