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 Protocols in Swift Protocol Basics Protocols With Methods

Chigozie Ofodike
Chigozie Ofodike
11,197 Points

What am I supposed to do here?

Im not very sure what i am supposed to do here

protocols.swift
// Declare protocol here
protocol ColorSwitchable {
  func switchColor(_ color: String )
}

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( _)

  init() {
    self.state = .on
    self.color = .rgb(0,0,0,0)
  }
}
JS Goupil
JS Goupil
11,297 Points

So your wifiLamp class is now conforming to ColorSwitchable. You've already created the protocol, and added the required function which is great. Now you have to implement the code for the function switchColor in the WifiLamp class. I'm assuming that a switch statement on the Color enum is going to be necessary. But start by trying to complete the function in the class.

1 Answer

Hi There,

First off the first task states "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 external argument label is omitted by using an underscore. Give the argument a local name of color." You have create the right ColorSwitchable, however the argument for the method "switchColor"needs to be of the type "Color" but you have made it type String.

For the second Task it asks "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." You have successfully adopted the ColorSwicthable protocol all you need to do now is set the color of the lamp stored in the WifiLamp property "color" to the color argument from the protocol function. As you need to refer to the stored property "color" within it's class you need to refer to it using the keyword self and assign the color argument to it. If I've confused you in anyway sorry, I've put my completed code below, hope that helps.

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
}

}