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

Christoph Hellmuth
Christoph Hellmuth
11,604 Points

I declare everything here but there is still the error that I am not following the directions: protocol ColorSwitchable

I declare everything here but there is still the error that I am not following the directions: protocol ColorSwitchable { func switchColor (color:Color) }

2 Answers

Nathan Tallack
Nathan Tallack
22,159 Points

So in the first step you define the protocol. It will look something like this:

protocol ColorSwitchable {
  func switchColor(color: Color)
}

In the second step you need to apply this protocol to your WiFiLamp class. You do that by popping it in after the Class name like this:

class WifiLamp: ColorSwitchable {

Now your class needs to have that function defined to comply with the protocol, so you will add the function in your class that will look like this:

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

So you need to remember that the protocol defines WHAT is required, then when you apply that to something like a class you need to then implement what is required in that class to comply.

Christoph Hellmuth
Christoph Hellmuth
11,604 Points

Thank you :) I still couldn't find what the error was, espacillialy that in my playground everything worked perfectly.

juzek
juzek
16,109 Points

There is a missing space, I've just had the same issue. Compare: func switchColor(color: Color) func switchColor(color:Color)