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
Kuang-Wen, Huang
2,525 PointsIssue with task of "Swift Recap Part 2" code challenge in "Build a Simple iPhone App with Swift 2.0"
Hello, I'm not sure what's the problem with my codes, cause I just learned the oriented object last week. Please help me to figure it out. Thanks a lot!
In the editor you've been provided with two classes - Point to represent a coordinate point and Machine. The machine has a move method that doesn't do anything because most machines are motionless.
Your task is to subclass Machine and create a new class named Robot. In the Robot class, override the move method and provide the following implementation. If you enter the string "Up" the y coordinate of the Robot's location increases by 1. "Down" decreases it by 1. If you enter "Left", the x coordinate of the location property decreases by 1 while "Right" increases it by 1.
Note: If you use a switch statement you can use the break statement in the default clause to exit the current iteration.
```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
var enter: String = "none"
class Robot: Machine {
override func move(direction: String) {
func move(enter: String) -> Point {
switch enter {
case "Up":
location = Point(x: 0, y: 1)
return location
case "Down":
location = Point(x: 0, y: -1)
return location
case "Left":
location = Point(x: -1, y: 0)
return location
case "Right":
location = Point(x: 1, y: 0)
return location
default:
print("dont move")
} } } }
enter = "Up"```
2 Answers
kevin kurashewich
14,120 Pointsi think this might work a little better for you
class Robot: Machine {
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: "dont move"
}
}
}
hopefully that works for you!!!
kevin kurashewich
14,120 Pointswoah that didn't look like that when i posted it lol
Kuang-Wen, Huang
2,525 PointsHaHa, that's my another question too. I don't know how to type syntax in a fine way in this community either. Is there anyone who can tell us how to type syntax here correctly?
Kuang-Wen, Huang
2,525 PointsKuang-Wen, Huang
2,525 Pointsoh yessssssss! It works! Thanks a lot! I forget how to call the x and y from the location which is type of Point. Now I get it, it looks like location.x and location.y .