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

Marcelo Lanzuolo
Marcelo Lanzuolo
12,154 Points

Protocols in Swift 3. Task 1 and 2 a bit confusing

Hi there. Please I've already tried these tasks many times but it is not working. With playground everything is fine. When I try to use the "_" to omit the external argument label it does not work anymore, both tasks.

Any suggestions? Tks in advance.

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.

// task 1 protocol ColorSwitchable { func switchColor(color: Color) }

//task 1

enum LightState { case On, Off }

enum Color { case RGB(Double, Double, Double, Double) case HSB(Double, Double, Double, Double) }

//task2 - WifiLamp using the protocol class WifiLamp: ColorSwitchable { let state: LightState var color: Color

init() {
    self.state = .On
    self.color = .RGB(0,0,0,0)
}
//task 2
func switchColor(color: Color) {
    self.color = color
}

//task 2
}

protocols.swift
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)
    }



}

4 Answers

David Papandrew
David Papandrew
8,386 Points

I just tried this code and it passed (with underscore for external name in protocol and class method).

Hope it works for you.

// 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
  }
}
Kanish Chhabra
Kanish Chhabra
2,151 Points

Why have you used self before 'color' in the 'switchColor' method?

s t
s t
2,699 Points

Hi, I am still having trouble understanding how this method actually changes the color of the light by using self.color = color. How does this change it from the rgb to hsb or am I misunderstanding the question? Can someone please explain this to me and also if we wanted to change the state of the LightState to off can someone show me the code to do that along with the protocol included? I just want to fully grasp these concepts before moving on. Thanks

I am also having trouble with this one... I cant 'resoulve. Please can someone help me. Thanks.

Marcelo Lanzuolo
Marcelo Lanzuolo
12,154 Points

Hi David. I've just tried again and it's worked. Thanks for your help.