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

Getting this to take value of enum Color as argument

So i am doing this challenge and have tried running this with a switch statement i thought was right and then this maybe right if else. What im a little confused on is how to take the value of color as an argument. The wording on this is tripping me up

protocols.swift
// Declare protocol here
protocol ColorSwitchable {
func switchColor (_ color) _> String
if bannanas = Color.rgb(23, 23, 23, 23)
print("bum") else print ("cool")


}


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

1 Answer

Nick Frozz
Nick Frozz
31,289 Points

Part 1/2 of this challenge is asking us to create a protocol named ColorSwitchable; You got it:

protocol ColorSwitchable {
}

Then it requires a func named "switchColor" with an omitted external argument "_", argument named "color" that has a value of "Color" inside of it, so you almost got it but forgot about a value. After 2nd step we're getting:

protocol ColorSwitchable {

func switchColor(_ color: Color)

}

Part 2/2. Asking us to adopt the ColorSwitchable protocol. To conform the protocol we need to use EVERY method and argument that our protocol has. In our case, it's a single function "switchColor". (function always goes before init.) :

class WifiLamp: ColorSwitchable {

let state: LightState

var color: Color

func switchColor(_ color: Color) {

self.color = color    //here we are simply setting a color to the new one.

}

init() {

self.state = .on
self.color = .rgb(0,0,0,0)

}
}