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 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.

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



}
Rogier Nitschelm
seal-mask
.a{fill-rule:evenodd;}techdegree
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
missgeekbunny
missgeekbunny
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

missgeekbunny
missgeekbunny
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

switchColor(Blue)

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.