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

Paul Je
Paul Je
4,435 Points

[SOLVED] Still stuck..

Can't set the colour of the lamp in the implementation of the method. Is the implementation on the call statement at the very end with Var? Or is it in the func section within the WifiLamp class that I would have to implement the new colour? I tried both but not working.. Thanks for any help!

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(state: LightState, color: Color) {
        self.state = .On
        self.color = .RGB(0, 0, 0, 0)
    }

    func switchColor(color: Color) {
        self.color = color
    }

}


var changedWifiLight = WifiLamp(state: .On, color: .RGB(20.0, 30.0, 20.0, 1.0))

4 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

I passed the challenge using all your code except the last name where you declare another variable. The code should look like this:

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

Moderator Edited: Added markdown to response so it can be read properly in the Community.

Paul Je
Paul Je
4,435 Points

For future readers, the function should be declared within WifiLamp in order to declare it as the protocol type in addition to Cindy's code

MIchael Montoya
MIchael Montoya
3,222 Points

This is the correct answer for only the first part of the problem. (1 of 2)

Paul Je
Paul Je
4,435 Points

Figured it out!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! lol

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Was just about to type out the answer/explanation for you. :)

Glad you got it figured out! Marked your Title as solved.

:dizzy:

Paul Je
Paul Je
4,435 Points

Still doesn't work lol, now I'm a little more confused however - it's weird since one other member mentioned the func function is required to pass... but yours doesn't include it, nor the type declared in the protocol to be the one for WifiLamp

Paul Je
Paul Je
4,435 Points

Driving me crazy lol. I'm on task 2, which probably should have been said initially sorry