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

Protocols in Swift 3

I have looked at others code and nothing seems to work. Here is the challenge:

In the editor below, I've declared a class, WifiLamp, that represents an interface to one of those Internet of Things lamps. The class models 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 external argument label is omitted by using an underscore. Give the argument a local name of color.

protocols.swift
// Declare protocol here
protocol ColorSwitchable {    // declared protocol
func switchColor(_ color: Color) {
  } // method with ommitted external label and color as the internal label
}

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)
  }
  func switchColor(_ : Color)
}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Matt Baldwin,

Try removing the curly braces from the switchColor declaration in the ColorSwitchable protocol.

// Declare protocol here
protocol ColorSwitchable {
    func switchColor(_ color: Color)  
    // Remember that protocols are just guidelines not implementations
}

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

EDIT: I had to make a change because I did not see that you added the switchColor method to the WifiLamp class and removed it. You will most likely do that in stage 2 when you make WifiLamp conform to ColorSwitchable.

Good Luck