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 trial

iOS Swift 2.0 Protocols Swift 2.0 Protocols Protocols With Methods

Heather Mathewson
Heather Mathewson
10,912 Points

WifiLamp and ColorSwitchable

"For this task, make WifiLamp adopt the ColorSwitchable protocol. In the implementation of the method, simply set the color of the lamp to the new color."

I am struggling with how to do this. Also, they seemed to got rid of the "Preview" option for the code today, which usually helps me out a little when I get stuck.

protocols.swift
// Declare protocol here


enum LightState {
  case On, Off
}

enum Color {
  case RGB(Double, Double, Double, Double)
  case HSB(Double, Double, Double, Double)
}

class WifiLamp {
  let state: LightState
  var color: Color

  init() {
    self.state = .On
    self.color = .RGB(0,0,0,0)
  }
}

protocol ColorSwitchable {
  func switchColor(color: Color)
}

WifiLamp(ColorSwitchable)

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Heather Mathewson,

You've set up your protocol properly, now we just need to make the WifiLamp class adopt this protocol and provide its implementation. The challenge is asking us to set the color of the lamp to the new color we are passing into our method. To do this, in the body of our method we would assign the color we are passing in as the parameter to self.color which refers to the instance of color that this method is being called on.

To declare that this class adopts the ColorSwitchable protocol, you need to write a colon after the class name and then specify the name of the protocol. This is the same syntax as subclassing another class. However, if this class was already being subclassed or already adopted another protocol, we would adopt multiple protocols after listing the first by separating them with commas.

// 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

  init() {
    self.state = .On
    self.color = .RGB(0,0,0,0)
  }

  func switchColor(color: Color) {
    self.color = color
   }
}

Good Luck!

Heather Mathewson
Heather Mathewson
10,912 Points

Thank you! It worked! Also, thank you for your explanation--I think I understand what they were asking a bit better now.

Sheng Wei
Sheng Wei
4,382 Points

I get the idea, but Im a little confused as to which color belongs to the protocol and the WifiLamp Class, mind clarifying that Steven?

Steven Deutsch
Steven Deutsch
21,046 Points

The protocol just defines a method called switchColor, which takes in a Color argument. The wifi lamp conforms to the ColorSwitchable protocol, so we know it will have this switchColor method. The implementation of this method simply changes the color of the wifi lamp instance its called on to the color being passed in to the method call. When you create an instance of WifiLamp, the default value for the color property will be Color.RGB(0,0,0,0). That is, until someone calls the switchColor method on the instance and passes it a different color.