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 Getting Started with iOS Development Swift Recap Part 2

Jason Oliverson
Jason Oliverson
4,230 Points

This compiles, but I get an error that it doesn't and the preview shows nothing. What am I missing?

I've tried this with and without the apostrophe in the "I'm a machine" string, but neither of them work. This is getting really frustrating because nobody seems to be able to tell me exactly what's wrong or what I should do to fix it

robots.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() {
        super.init()

    }



    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
        }
    }
}

1 Answer

Kevin Fricovsky
PLUS
Kevin Fricovsky
Courses Plus Student 14,460 Points

Hey Jason,

Taking a look at the code provided in this challenge, the original move() method defined in Machine looks like this:

func move(_ direction: String) {
    print("Do nothing! I'm a machine!")
}

I was able to complete the challenge using your code by adding an underscore before the first argument in both the move method defined in Machine and the override method in Robot. The underscore tells the compiler that there should not be an argument label for the first parameter in the method.

It's the difference between calling the method like this:

func move(_ direction: String) {
    print("Do nothing! I'm a machine!")
}
move("Up")

and this:

func move(direction: String) {
    print("Do nothing! I'm a machine!")
}
move(direction: "Up")

I'll bet the test harnessed failed your code because you were requiring a label for the argument "direction".

Hope this helps!