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

Jeff Ripke
Jeff Ripke
41,989 Points

Where am I going wrong here?

What do I put in the body of switchColor?

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

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

    func switchColor(color: Color) {

    }
}

1 Answer

Nathan Tallack
Nathan Tallack
22,159 Points

You have done great! You were very close. All that was missing was taking the parameter passed into that function and assigning it to the self.color value. This will allow you to use that method on the object to change its color in the future. This is because the practice is not to set the properties on an object directly but to set them using a method, as the protocol you defined requires.

See your code below with the missing line.

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