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

Can't adopt this protocol

I've asked this same question earlier but not a response yet, hopefully someone can help? I'm trying to adopt a new protocol to a class. I setup the protocol and the method that the class will have to conform to. However, I can't figure out how to implement the function into the class?

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

  init() {
    self.state = .On
    self.color = .RGB(0,0,0,0)
  } 
  func switchColor(color: Color) {
        let newColor = Color.HSB(255, 255, 255, 255)
    }
}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

You are doing good, the only thing is that the challenge is not asking you to create an instance of "Color" as you are doing in the function.

What you want to do is change the "color" property within the class. Meaning that if you create an instance of wifiLamp, then you will be able to call on that function and change the property.

    func switchColor(color: Color) {
        self.color = .RGB(2.0,2.0,2.0,1.0)
    }

Good luck

I was trying everything else except for the "self.", Thank you. I need to study the "self." more, i'm also revisiting functions.

Jhoan Arango
Jhoan Arango
14,575 Points

self is simple.. when you see that there is a conflict of names, then make sure to use self. In this case self.color is being used because the function has a parameter named "color" the same as the property "color" from the class. So we want to specify self.color to let the system know that we are referring to the property and not the parameters name from the function.

It's still no passing, however, the code does work in Xcode?

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)

    }
    func switchColor(color: Color) {
        self.color = .RGB(2.0,2.0,2.0,1.0)
    }
}

This is what I have.

Jhoan Arango
Jhoan Arango
14,575 Points

You forgot to adopt the protocol

That's what I'm not understanding how to do?

I figured it out! I was forgetting to add :ColorSwitchable to the class. I didn't understand how to do it until I revisited the video. Thanks!

Jhoan Arango
Jhoan Arango
14,575 Points
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)
}

// Adopt protocol by adding the protocol to the class. :ColorSwitchable 

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 = .RGB(2.0,2.0,2.0,1.0)
    }
}