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

Lana Wong
Lana Wong
3,968 Points

What is wrong with the declaring of my method?

INSTRUCTIONS: For the first step, declare a protocol named ColorSwitchable. The protocol has a single requirement: a method named switchColor that takes a value of Color as an argument. For the sake of this challenge, make sure your external argument label is omitted by using an underscore. Give the argument a local name of color.

I do not understand what is wrong with my switchColor method

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

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

There are 2 things I see wrong here. First of all, there needs to be a space between the underscore (_) and the color parameter name. In Swift, and underscore (_) is just a placeholder for nothing in many cases, such as variable and parameter names. However, it's its own distinct symbol, and needs to be separated from all other symbols. As it is, your declaration is for a function called switchColor(_color:), but the question asks for a function called switchColor(_:). Furthermore, function declarations in protocols cannot have bodies, so the extra opening and closing brackets after your declaration are not necessary