iOS Build a Simple iPhone App with Swift Getting Started with iOS Development Swift Recap Part 2

What's not working here?

When I copy paste into a playground the code compiles, but in browser Treehouse says it can't compile but provides no error message in output. I ran a few print tests on this in my playground and it seemed to produce the right functionality.

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! I am 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: print("Plese enter a valid direction")
        }
    }
}

4 Answers

andren
andren
28,466 Points

The problem is that you have (accidentally?) changed a part of the code you were not meant to touch. The move method that you are overriding has a _ as an external parameter name when the challenge loads, but in your code that has been removed.

If you add it back and also add it to the overridden method (since they have to be identical) like this:

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) { // Added _ as external parameter name
        print("Do nothing! I am a machine!")
    }
}

// Enter your code below

class Robot: Machine {

    override init() {
        super.init()
    }

    override func move(_ direction: String) { // Added _ as external parameter name
        switch direction {
        case "Up": location.y += 1
        case "Down": location.y -= 1
        case "Left": location.x -= 1
        case "Right": location.x += 1
        default: print("Plese enter a valid direction")
        }
    }
}

Then the code will be accepted.

Patrick Duhamel
Patrick Duhamel
5,786 Points

When you override a function, it must match the signature of the original function in the code presented in the recap, which means that the function move doesn't have an external parameter with the symbol _

func move(_ direction: String) {...} , so your code needs to match it by writing it as

override function move(_ direction: String) {...}

Okay got it, thanks for the help! Does the parameter _ have some significance, either functionally or conventionally?

andren
andren
28,466 Points

It does actually have some functional significance. It makes it so that you can call the function/method without having to specify a parameter name as you normally would. Take this function:

func addTwoNumbers(num1: Int, num2: Int) -> Int {
  return num1 + num2
}

The name of the function makes it pretty obvious what the two arguments are meant to be, so having to specify the parameter labels when calling the function like this:

addTwoNumbers(num1: 5, num2: 10)

Is a bit unnecessary since it doesn't really make the code any easier to read, which is partly why parameter labels exist in the first place. For situations like this Swift allows you make it so that you can call the function without specifying parameter names, and that is done with the _ external parameter name.

So if the function looked like this:

func addTwoNumbers(_ num1: Int, _ num2: Int) -> Int {
  return num1 + num2
}

Then you could call it without specifying any labels like this:

addTwoNumbers(5, 10)

Great, thank you! That clears it up.