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

Steven Holt
Steven Holt
7,382 Points

These directions are incredibly vague... Also there is no "Preview" button on this code challenge... Completely stuck.

Am I the only one?

Jeff McDivitt
Jeff McDivitt
23,970 Points

There is something wrong with this challenge. I will send to Treehouse Moderator

1 Answer

Arman Arutyunov
Arman Arutyunov
21,900 Points

I can't agree with you. The directions are pretty much straightforward. All you need to do is to declare a protocol that contains a blueprint of a function that have to be implemented in any class that adopts the protocol.

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

And then you are asked to set WifiLamp class adopted by the protocol. When you adopt a protocol you have to implement all its methods that are not optional. In our case a switchColor method is required, so just implement it with setting a property color to any value. Color property is of type "Color" (and that is an enum). You just choose .rgb or .hsb and set it to any value you want.

class WifiLamp: ColorSwitchable {
  let state: LightState
  var color: Color

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

// here you just implement the function
  func switchColor(_ color: Color) {
    self.color = .rgb(255,255,255,1) 
  }
}

Hope it helps