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 2

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

please review my code im stuck

Hi,

Im confuse with this challenge. why i cant call location to the while loop function?

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! I'm a machine!")
    }
}

// Enter your code below

class Robot: Machine {

    override init() {
        super.init()
    }

    override func move(direction: String) {


        switch direction {
            case "Up" : return (location.y + 1)
            case "Down": return (location.y - 1)
            case "left": return (location.x - 1)
            case "right" : return (location.x + 1)
            default: break
        }
    }
}
Berry Loeffen
Berry Loeffen
4,303 Points

Hi there,

Writing location.y + 1 is the issue.

In this code you did not add 1 to the y coordinate.

NEW location.y = location.y + 1

Or use the compound operator instead:

class Robot: Machine {

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

        default: break
        }}}

There are 2 ways to solve this challenge. With a switch statement like above or with an if statement.

Good luck!

1 Answer

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

Hi Thx alot for the answer it helps!

I have questions if you dont mind.

  1. how can you know to use += instead of location.y + 1. I know its different but I dont know when to apply it.
  2. from your answer you don't make another init for the sub class? if im not mistaken class always have to have an init to declare right? why the test is pass eventhough it doesnt have init? is it considerable inherite the init from the super class? eventhough we not declaring it?

Thank You

Berry Loeffen
Berry Loeffen
4,303 Points
  1. For example in the case "Up", you want to have the y coordinate go up by 1.

So in this example the point on the map is x:0 and y:0. (Subclass inherits location (x:0,y:0) from superclass)

you want your new location to be x:0,y:1

so your new location will be -> (new)location.y = (old)location.y + 1

now your (new)location.y is 1. so the coordinate point now is x:0 and y: 1

writing the += compound operator as it is called takes the burden away of having to write location.y = location.y +1.

in stead you can use the += compound operator (location.y += 1) so you don't have to write location.y again. -> Less typing is less work, and both location.y = location.y + 1 and location.y += 1 are the same thing.

  1. Yes in classes you need initializers. in this case it would be
override init() {
        super.init()
    }

But since the superclass had a fixed value for point (x:0,y:0) for its stored property as initialized in the superclass Machine, in this case you can omit it. What actually happens is that the subclass Robot has the same position (x:0,y:0)