Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

I am stuck in the code challenge of the URL https://teamtreehouse.com/library/build-a-simple-iphone-app-with-swift-20/g

I am stuck in the code challenge of the URL https://teamtreehouse.com/library/build-a-simple-iphone-app-with-swift-20/g

My code is as presented below. Could someone please 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{

override init() { super.init() self.location = Point(x: 10, y: 10) }

override func move(direction: String){

  switch direction{

   case "Up": self.location.y = self.location.y++ 
    case "Down": self.location.y++
    case "Left": self.location.x--
    case "Right": self.location.x++
    default: break

  }

}

}

3 Answers

David,

Try not overwriting init.

If you still have trouble, try copying your code into an XCode playground and see what errors come up.

For the complete answer, check this post: https://teamtreehouse.com/community/moving-method

Florin

Thanks Florin. It did work but why was I not supposed to override the init method?

David,

The challenge did not specify that the location property needed to be different in the Robot class than in the parent class. When these challenges run on the teamtreehouse.com servers, they expect the location property not to be changed from what is defined in the parent class.

If you do not change the value of the location property in the parent class, and you shouldn't in the challenge, and you do not define any properties in the Robot class, there is no need to override the init method. It would be redundant as all the properties and methods defined in the parent class are available to the child class

Let me know if that needs more explanation.

Florin