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

}