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

Duong Nguyen
Duong Nguyen
3,292 Points

Help with my code

My code is:

class Robot: Machine { func move(_ direction: String) -> Point {

    switch direction {
        case "Up": location.y += 1
        case "Down": location.y -= 1
        case "Left": location.x -= 1
        case "Right": location.x += 1
    default: break
    }
    return location
}

}

and Bummer!, but there are no compiler errors in Preview! Any help?

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

// Enter your code below


  class Robot: Machine {
       func move(_ direction: String) -> Point {

        switch direction {
            case "Up": location.y += 1
            case "Down": location.y -= 1
            case "Left": location.x -= 1
            case "Right": location.x += 1
        default: break
        }
        return location
    }

}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Duong,

You've almost got it, but there are a few things:

  1. You are indicating a return type of Point for the function, but the Task does not ask for this and the original function has a Void return value, so that needs to be deleted.
  2. You are missing the override keyword.
  3. At the end, you are returning location, but the return value of the function needs to be Void, so nothing can be returned, so this line also need to be deleted.

Fix those up and you're good to go. :)

Keep Coding! :dizzy:

Duong Nguyen
Duong Nguyen
3,292 Points

Yesss, thanks Jason, it finally worked.

Just for the sake of practicing, how can I test the result of the new method, like how do you print out the location after inputting the string "Up"? I tried these codes in Xcode, but it did not show the new coordinates:

let robot = Robot()

robot.location = Point (x: 1, y: 3)

robot.move(direction: "Up")

print(robot.location)