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 trialChung-En Hsu
3,761 PointsDon't know how to fulfill the task
In this challenge task, I'm a little confused about the question, I'm not sure I really understand this... My code is as follows:
class WifiLamp: ColorSwitchable { let state: LightState var color: Color func switchColor(color: Color) -> () { color = color }
In fact, I have no idea what I should do... Can anyone plz tell me? Thanks a lot!!
// Declare protocol here
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
func switchColor(color: Color) -> () {
.color = color
}
init() {
self.state = .On
self.color = .RGB(0,0,0,0)
}
}
2 Answers
Martin Wildfeuer
Courses Plus Student 11,071 PointsThe problem is the following code:
func switchColor(color: Color) -> () {
.color = color // .color is a syntax error
}
This would not do either, as your function parameter name is the same as your instance variable name color
.
func switchColor(color: Color) -> () {
color = color // parameter name == variable name
}
In order to let Swift know that you want to set the member variable color
of your class, you have to explicitly refer to it with self
, so it's not ambiguous, as, again, your method parameter has the name as your instance variable.
Setting the variable color
of in instance of your class WifiLamp
would be:
func switchColor(color: Color) -> () {
self.color = color // set color var
}
Hope that helps :)
Chung-En Hsu
3,761 PointsThank you very very much!!! It helped a lot!!!