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

boris said
boris said
3,607 Points

Challenge saying code can't be compiled; no errors in X-CODE or preview

Any help would greatly appreciated.

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!")

    }
    }

    class Robot: Machine {
        var moveDirection: String
        init(moveDirection: String) {
            self.moveDirection = moveDirection
            super.init()
        }
       override func move(moveDirection: String) {
        switch moveDirection {
            case "Up": print("The robot moved up one y coordinate")
            location.y += 1
            case "Down": print("The robot moved down one y coordinate")
            location.y -= 1
            case "Right": print("The robot gained one x coordinate")
            location.x += 1
            case "Left": print("The robot lost one x cordinate")
            location.x -= 1
        default: print("Machines don't move like that...")

        }
        }


}

let somethignOrAnother = Robot(moveDirection: "Up")

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Boris,

There's no need to add a property to the Robot class. We want our robot to be able to move in any direction! The only thing that needs to be changed is the move function.

If you simply remove that extra property, and the init method (we don't need to change anything there either), you'll be good to go (your last line of code won't work after these changes).

One last point: I really encourage you to pay close attention to spacing and indentation. Your code is quite difficult to navigate, because it's hard to tell where classes, functions, switch statements, etc. begin and end.

Cheers :beers:

-Greg

boris said
boris said
3,607 Points

If I don't have a property, what do you recommend I switch in my switch statement?

Greg Kaleka
Greg Kaleka
39,021 Points

Just leave the function parameter called direction and switch on that.