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 Architecture of an iOS App

Code Challenge: Swift Recap Part: 2 (HELP)

Dear Answers,

Can some help me understand how to solve this?

As my mind is rusty with this type of info.

Thanks, JC

1 Answer

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!")
    }
}
class Robot: Machine{

    override init()
    {
        super.init()
        self.location = Point(x: 0, y: 0)
    }
    override func move(direction: String) {
        switch direction{
        case "Up":
            location.y += 1
        case "Down":
            location.y -= 1
        case "Right":
            location.x += 1
        case "Left":
            location.x -= 1

        default:
            print("Not a valid command!")
            break
        }

    }
}

this is my solution to the challenge, basically you got to create the subclass Robot, then as we know there are 2 steps in creating a subclass

  1. initialize all subclass properties (in this case Robot)
  2. initialize all superclass properties (in this case Machine)

since i did not add any properties the "override init()" just initializes the properties from Machine that is the location property.

the task is to override the Move method (also called function) on the Robot subclass so we call the method "override func move(direction: String) {". then proceed to add the code to move the location of the robot depending the value of an String var called "direction". if it goes "Up", Y value of location increases by 1 and a "Down" value decreases the value of Y by 1.

you cant test this code in a playground (i used Xcode 7.3) adding these lines below the code

var robot = Robot ()
robot.location
robot.move("Up")
robot.location
Niilo Pirttijärvi
Niilo Pirttijärvi
Courses Plus Student 2,354 Points

Hi, Could you open up a little bit what this part of the code does and especially why is it needed? override init() { super.init() self.location = Point(x: 0, y: 0) } Since the subclass Robot inherits everything from superclass Machine, why does it not inherit its location and initialisation as well?