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

Napatchol Thaipanich
PLUS
Napatchol Thaipanich
Courses Plus Student 13,534 Points

The ColorSwitchable protocol needs to have a method requirement as specified in the directions.

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

1 Answer

Matthew Connolly
seal-mask
.a{fill-rule:evenodd;}techdegree
Matthew Connolly
iOS Development Techdegree Student 11,595 Points

Hi Napatchol,

Protocol looks great, just a small tweak is needed! The instructions ask you to "... make sure your external argument label is omitted by using an underscore."

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

More information here (Stack Overflow).

Otto linden
Otto linden
5,857 Points

Why do you need to do that? 🙂