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 trialFredrik Carlsson
4,248 PointsThe code does not work :(
Can someone see what's wrong?
class Point {
var x: Int
var y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
class Machine: Point {
override init(x:Int, y: Int) {
super.init(x: x, y: y)
}
func move(_ direction: String) {
print("Do nothing! I'm a machine!")
}
}
class Robot: Machine {
override func move(_ direction: String) {
if direction == "up"{
self.y+=1
}
if direction == "down"{
self.y-=1}
if direction == "left"{
self.x-=1
}
if direction == "right"{
return self.x+=1
}
}
1 Answer
miikis
44,957 PointsHey Fredrik,
When you subclass something, you have to call the parent class' initializer. Also, consider using a switch statement in move:direction. If you don't feel like it, at the very least, you'll want to make sure that you're not returning anything from the function.