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

Chung-En Hsu
Chung-En Hsu
3,761 Points

Don't know how to fulfill the task

In this challenge task, I'm a little confused about the question, I'm not sure I really understand this... My code is as follows:

class WifiLamp: ColorSwitchable { let state: LightState var color: Color func switchColor(color: Color) -> () { color = color }

In fact, I have no idea what I should do... Can anyone plz tell me? Thanks a lot!!

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) -> () {
  .color = color
  }

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

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

The problem is the following code:

func switchColor(color: Color) -> () {
  .color = color // .color is a syntax error
}

This would not do either, as your function parameter name is the same as your instance variable name color.

func switchColor(color: Color) -> () {
  color = color // parameter name == variable name
}

In order to let Swift know that you want to set the member variable color of your class, you have to explicitly refer to it with self, so it's not ambiguous, as, again, your method parameter has the name as your instance variable.

Setting the variable color of in instance of your class WifiLamp would be:

func switchColor(color: Color) -> () {
  self.color = color // set color var
}

Hope that helps :)

Chung-En Hsu
Chung-En Hsu
3,761 Points

Thank you very very much!!! It helped a lot!!!