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 trialDuong Nguyen
3,292 PointsHelp 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?
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
Treehouse Moderator 145,860 PointsHey Duong,
You've almost got it, but there are a few things:
- You are indicating a return type of
Point
for the function, but the Task does not ask for this and the original function has aVoid
return value, so that needs to be deleted. - You are missing the
override
keyword. - At the end, you are returning
location
, but the return value of the function needs to beVoid
, so nothing can be returned, so this line also need to be deleted.
Fix those up and you're good to go. :)
Keep Coding!
Duong Nguyen
3,292 PointsDuong Nguyen
3,292 PointsYesss, 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)