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

Tyler Dotson
PLUS
Tyler Dotson
Courses Plus Student 1,740 Points

I am confused I did what the directions wanted and if I didnt someone please tell me what I missed.

I am just confused.

protocols.swift
// 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 {
  let state: LightState
  var color: Color

  init() {
    self.state = .on
    self.color = .rgb(0,0,0,0)
  }
}

1 Answer

Andrew Boryk
Andrew Boryk
15,916 Points

Hi Tyler,

The challenge asks for you to create a protocol called, ColorSwitchable, which you did. Then it asks that you add a function to the protocol called switchColor(color:), which you did. The only part that needed to be corrected, is for you to remove the curly braces after you add the function to the protocol. Inside the protocol, there is no need to define the function, the function will be defined in the type which inherits the protocol. Thus, this would be the answer for the top:

// Declare protocol here
protocol ColorSwitchable {
    func switchColor(_ color: Color)
}

And for the second part of the task, the challenge asks you to have the WifiLamp adopt the ColorSwitchable protocol, and to set the color received in the function to the color property of the WifiLamp. This can be achieved like so:

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

Hope this helps!