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 Build a Simple iPhone App with Swift 2.0 Getting Started with iOS Development Swift Recap Part 1

Do I need a 'self' here?

For the second part of this challenge, it asks me to add the subclass Robot with the functionality to move. My code is as follows:

class Machine {
    var location: Point

    init() {
        self.location = Point(x: 1, y: 1)
    }

    func move(direction: String) {
        print("Do nothing! Im a machine!")
    }
}

class Robot: Machine {
    override init() {
        super.init()
    }

    override func move(direction: String) {
        switch direction {
            case "Up": location.y = location.y + 1
            case "Down": location.y = location.y - 1
            case "Left": location.x = location.x - 1
            case "Right": location.x = location.x + 1
            default: break
        }
    }
}

My question is in the switch statement, do I need to say

self.location.y = self.location.y + 1

or is it fine the way it is?

1 Answer

Moritz Lang
Moritz Lang
25,909 Points

Just use self.location.y += 1 instead. :)