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

Erik Faye Karla
Erik Faye Karla
1,523 Points

so how do i change the x and y coordinants??

i want to change the place robot is in but I dont know what code I need to do that!

classes.swift
class Point {
    var x: Int
    var y: Int

    init(x: Int, y: Int){
        self.x = x
        self.y = y
    }
}


class Machine {
    var location: Point

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

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

// Enter your code below
class Robot: Machine {
    override init(){
        self.location = Point(x: 1, y: 1) 
    }
    override func move(direction) -> String {
        switch direction {
            case "Up": Robot.y = y + 1 
            case "Down": Robot.y -= 1 
            case "Left": Robot.x -= 1
            case "Right": Robot.x += 1 
            default: break 
        }
    }
}

2 Answers

Erik,

You do not need to override init in this one, because you do not define any properties, so you do not need to initialize them. Everything will get initialized with the init defined in the Machine class.

When you override the move method, you should not have the String return, as it is not returning anything. I believe you may have misplaced it and you should place it as the type of the direction parameter.

override func move(direction: String)

Lastly, instead of Robot.y or Robot.x, it should be self.location.y and self.location.x

See my complete answer in another post I recently answered, if you're still having trouble.

Florin

Erik Faye Karla
Erik Faye Karla
1,523 Points

thank you! After I figured out I could press the swift recap 2 over my question i found the post you linked! thanks anyway