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 trialkask
7,509 PointsHaving troubles.
In the editor below, I've declared a class, WifiLamp, that represents an interface to one of those Internet of Things lamps. The class has an state that determines whether the lamp is on or off and a color, represented by the Color enum. For the first step, declare a protocol named ColorSwitchable. The protocol has a single requirement: a method named switchColor that takes a value of Color as an argument. For the sake of this challenge, make sure your method parameter is named color.
4 Answers
Nathan Tallack
22,160 PointsOk, here is the total solution formatted correctly. Cut and paste this exactly and see what happens.
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
}
}
Nathan Tallack
22,160 PointsSo in the first step you define the protocol. It will look something like this:
protocol ColorSwitchable {
func switchColor(color: Color)
}
In the second step you need to apply this protocol to your WiFiLamp class. You do that by popping it in after the Class name like this:
class WifiLamp: ColorSwitchable {
Now your class needs to have that function defined to comply with the protocol, so you will add the function in your class that will look like this:
func switchColor (color: Color) {
self.color = color
}
So you need to remember that the protocol defines WHAT is required, then when you apply that to something like a class you need to then implement what is required in that class to comply.
kask
7,509 PointsYes but still getting: Bummer! The ColorSwitchable protocol needs to have a method requirement as specified in the directions. i've done what you did
Nathan Tallack
22,160 PointsVery strange. I just clicked on your challenge and cut and paste my code into each step and it worked perfectly.
Are you sure you are not mistyping something.
kask
7,509 Pointsprotocol ColorSwitchable { func switchColor (color: Color) { self.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)
}
}
Duncan Hall
6,417 PointsMore recently: https://teamtreehouse.com/community/the-colorswitchable-protocol-needs-to-have-a-method-requirement-as-specified-in-the-directions
There's a super useful explanation of underscore usage on stack overflow linked in the above post, and also here: https://stackoverflow.com/questions/39627106/why-do-i-need-underscores-in-swift/39627305#39627305
Jay Padzensky
4,731 PointsWhat browser are you using? Have you cleared its cookies and cache lately?
kask
7,509 PointsI'm using safari, i've tried google chrome, firefox as well cleared browser for chrome didn't work.
kask
7,509 Pointskask
7,509 PointsIt worked, thanks for your guys help