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

Magnus Hållberg
Magnus Hållberg
17,232 Points

Protocol code challenge 2/2

Im probably blinde to the problem so I cant figure this out, I have seen solutions to this challenge and I think that I have done this correctly but obviously not.

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: ColorSwitchable {
    let state: LightState
    var color: Color

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


    init() {
        self.state = .On
        self.color = .RGB(0,0,0,0)
    }
}

1 Answer

Christian Mangeng
Christian Mangeng
15,970 Points

Hi Magnus,

this one will work. Note that:

1) there was one additional closing bracket in your code, which closed the WifiLamp class too early

2) the challenge asked you to omit the external name for the switchColor method

3) you changed the letters in the words rgb, hsb, on and off to uppercase letters

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

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


    init() {
        self.state = .On
        self.color = .rgb(0,0,0,0)
    }
}
Magnus Hållberg
Magnus Hållberg
17,232 Points

Thank you! I have rewritten this challenge, seen the videos and looked at solutions so many times and still not geting it to work that I was ready to give up on this corse.