Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Protocols in Swift Protocol Basics Protocols With Methods

protocol challenge 2 - don't known what I'm doing wrong here

I thought i have already put in the method but the compiler keeps showing that the protocol ColorSwitchable needs a method as directed in the instructions???

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)
  }
}
douglas madson
douglas madson
Courses Plus Student 10,182 Points

In your switchColor function your external argument is "_color". The challenge wants the external argument omitted and the internal argument named 'color'. In order to omit an external argument for a function you would do something like this:

func someFunction(_ localArgument: Value)

Hope this helps!

2 Answers

douglas madson
PLUS
douglas madson
Courses Plus Student 10,182 Points

In your switchColor function your external argument is "_color". The challenge wants the external argument omitted and the internal argument named 'color'. In order to omit an external argument for a function you would do something like this:

func someFunction(_ localArgument: Value)

Hope this helps!

HI thank you for your answer but still can't seem to get code to work, if you are able to would you be able to give an example code for visual representation, it would be much appreciated Thanks

douglas madson
PLUS
douglas madson
Courses Plus Student 10,182 Points

So your switchColor function should look like this:

protocol ColorSwitchable { func switchColor(_ color: Color)

There is a space between the underscore character and "color".

Hope this helps