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 Swift 2.0 Protocols Swift 2.0 Protocols Protocols With Methods

stuck on 2nd part of challenge, I can't seem to find the "new color" it's asking for

Am i missing something on the challenge?

protocols.swift
// Declare protocol here
protocol ColorSwitchable {
  func switchColor(color: Color) -> Double
}

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) -> Double {
       return ??? /* I don't see a new color anywhere */
  }
}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey anthony fontes,

You're close! What you need to fix is the return type of your switchColor method and the body of the switchColor method. The return type of this method is Void, so you need to change that in the protocol and in the implementation of the WifiLamp class. The reason this method is returning Void is because we don't want any value back when we calling it. We simply want to change the value of the WifiLamp's color property to the Color we pass in when calling this method.

Inside the body of the switchColor method, you can refer to self.color, which will be the color property of the WifiLamp instance you are calling this method on. Then you assign it the value of the color argument you are passing in. This is very similar to how you assign a value to a property using an initializer. An initializer is simply a special kind of method!

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

Good Luck!

Thanks man!

OMG I was stuck on this one for ages too Anthony.