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 trialMitch Little
11,870 PointsWhat Do I Execute In the Switch Statement?
I have tried various identifiers to be executed on in the switch statement however I cannot figure it out. The value switched on may also be incorrect.
Thank you for any replies :)
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()
}
override func move(_ direction: String) {
switch direction {
case "Up":
case "Down":
case "Right":
case "Left":
default: break
}
}
}
5 Answers
Jeff McDivitt
23,970 PointsThat is correct
Jeff McDivitt
23,970 PointsHi Mitch -
You are switching on the proper value and you have the case values correct also. The instructions state you need to either increase or decrease by 1 for each switch value. That means that the location will change so that needs to be is your switch statement. You also do not need the override init and super init.
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:
break
}
}
}
Mitch Little
11,870 PointsGreat, really appreciate your help! I just forgot to add the binary operator!
If you have a moment, could you please explain why I do not need to override the init and super init? I thought that if a subclass was called the superclass had to also be initialised first, hence why I included the init method.
Thanks again for your help!
Jeff McDivitt
23,970 PointsFrom Apple Docs
Overriding
A subclass can provide its own custom implementation of an instance method, type method, instance property, type property, or subscript that it would otherwise inherit from a superclass. This is known as overriding.
To override a characteristic that would otherwise be inherited, you prefix your overriding definition with the override keyword. Doing so clarifies that you intend to provide an override and have not provided a matching definition by mistake. Overriding by accident can cause unexpected behavior, and any overrides without the override keyword are diagnosed as an error when your code is compiled.
The override keyword also prompts the Swift compiler to check that your overriding classβs superclass (or one of its parents) has a declaration that matches the one you provided for the override. This check ensures that your overriding definition is correct.
Also more information here if needed
Mitch Little
11,870 PointsOkay thank you :) So essentially it is used when modifying the initialiser in the subclass and when doing so, super.init must be used to provide values to the super class to also initialise it?