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

Farhan Khan
Farhan Khan
17,530 Points

protocols with methods challenge

protocols with methods challenge 2 - 2 how to solve it?

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)
  }
  func switchColor(color: Color){
  self.color = Color
  }

}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I received your request for assistance :email:. You're doing well. You declared the protocol and started building the function. However, you did not make your WifiLamp conform to the protocol. Also, in the self.color = Color you're trying to set the color property to a Color type. Take a look:

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

// enums removed to shorten the code displayed
class WifiLamp : ColorSwitchable {  // make the WifiLamp conform to the protocol you've declared.
  let state: LightState
  var color: Color

  init() {
    self.state = .on
    self.color = .rgb(0,0,0,0)
  }
  func switchColor(_ color: Color){  // The function's parameters must match the ones in the declaration of the protocol so the _ is needed here
    self.color = color  //This sets color to the color that was sent in which is of type Color
  }
}

Hope this helps! :sparkles: