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 trialKevin Gutowski
4,082 PointsWhy do we pass in an enum (Color) into ColorSwitchable's method (switchColor)?
Not sure I understand why we have to specify an instance of Color in the protocol when all we are doing is switching it "ON"
I'm not sure I understand the difference between these two sets of code. (The second one doesn't work)
1.
protocol ColorSwitchable {
func switchColor(color: Color)
}
enum LightState {
case On, Off
}
enum Color {
case RGB(Double, Double, Double, Double)
case HSB(Double, Double, Double, Double)
}
class WifiLamp: ColorSwitchable {
let state: LightState
var color: Color
init() {
self.state = .On
self.color = .RGB(0,0,0,0)
}
func switchColor(color: Color) {
self.color = color
}
}
2.
protocol ColorSwitchable {
func switchColor()
}
enum LightState {
case On, Off
}
enum Color {
case RGB(Double, Double, Double, Double)
case HSB(Double, Double, Double, Double)
}
class WifiLamp: ColorSwitchable {
let state: LightState
var color: Color
init() {
self.state = .On
self.color = .RGB(0,0,0,0)
}
func switchColor() {
self.color = color
}
}
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! It's because you're not just switching it on. When you create an instance of the object the initial values are a state of On and a color of black. But that method needs to accept an argument for when we change the color of the light. So if we want to change our default light to another color, it needs to know what color we want to change it to. Hope this makes sense!
Kevin Gutowski
4,082 PointsKevin Gutowski
4,082 PointsHmm perhaps its that I don't quite understand this part
I would understand if it was written self.color = .RGB(.3,.3,.3,1) or something but instead we just pass an enum to self.color? We never actually specify a color within the function switchColor. Thanks for your help btw Jennifer!
EDIT: Actually I think I understand now. When we initialize the class we are setting color: Color to be Color.RGB(0,0,0,0) so the variable color is of the correct format.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherKevin Gutowski because that would hardcode the color we're switching it to. Imagine for example that we're simulating one of those lights that oscillates colors. We need to be able to send in the color that it's switching to at that point in time. If we did it the way you're saying, that color would only ever be switchable to (3, 3, 3, 1). I hope this helps. I'm not sure I can explain it any better than this
Kevin Gutowski
4,082 PointsKevin Gutowski
4,082 PointsI appreciate your help Jennifer. I think the example isn't the greatest because we don't set initialization parameters but rather just default property values. (Moreover the .on, state has a color of black?) I just thought the function switchColor() would switch color values to a new color but that does't seem to be the case.