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 Swift 2.0 Protocols Swift 2.0 Protocols Protocols With Methods

Having 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
Nathan Tallack
22,158 Points

Ok, 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
  }
}

It worked, thanks for your guys help

Nathan Tallack
Nathan Tallack
22,158 Points

So 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.

Yes 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
Nathan Tallack
22,158 Points

Very 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.

protocol 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)
}

}

Jay Padzensky
Jay Padzensky
4,719 Points

What browser are you using? Have you cleared its cookies and cache lately?

I'm using safari, i've tried google chrome, firefox as well cleared browser for chrome didn't work.