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

Johnny Nguyen
Johnny Nguyen
3,875 Points

Struggle with Protocol Challenge

"make sure your external argument label is omitted by using an underscore". I don't understand this sentence.

// 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)

Rogier Nitschelm
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

Functions in swift can have an external argument. In your example:

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

here color is an external argument label. This means that if you are calling the function switchColor you will have to do it like this:

switchColor(color: SomeColor)

But if you leave off the external argument label, by using the underscore:

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

You will be able to call the function by doing this:

switchColor(SomeColor) // notice that the color:-part is missing
37,033 Points

For Rogier Nitschelm you should really use the reply box at the bottom instead of adding a comment. You have smart stuff to say but because you comment on the initial question instead of using the reply box it never counts as an answer which means people can't do things like upvote it or give it best answer. It also makes the system think that no one has answered the question.

1 Answer

37,033 Points

It's saying that it doesn't want color to be the external label as it would be in

func switchColor(color: Color)

It wants you to specify that when you call it instead of

switchColor(color: Blue)

you can just put


And you get that to occur by writing the code like this:

func switchColor( _ color: Color)

Then the underscore covers the external label so you don't need to put it to call the function and the color label can still be used inside the function. Hope that makes sense.