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 trialTodd Stacy
5,816 Pointsthe error says my code cannot be compiled, but there is no preview button to figure out why. can someone please help?
ive been working on this for more than an hour and i have no idea what could be wrong. theres no way i can debug this with out a preview button.
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)
init() {
self.state = .On
self.color = .RGB(0,0,0,0)
}
}
1 Answer
jcorum
71,830 PointsYes, they say there's a Preview button, but there isn't.
class WifiLamp: ColorSwitchable {
let state: LightState
var color: Color
init() {
self.state = .On
self.color = .RGB(0,0,0,0)
}
func switchColor(color: Color) {
self.color = color
}
}
Usually functions are written after the initializer. Not required. But a good idea. When you create a parameter for a function it needs a name, here color and a type, here Color. Note that the type must start with a capital letter. The function also needs to change the stored property color to the color passed in to the function (and stored in a local variable). Because the local variable and the stored property have the same name, color, you need to use self to disambiguate them. Think of self as meaning my. So the function is saying make my color property have the color passed in to the function.
Todd Stacy
5,816 PointsTodd Stacy
5,816 Pointsthank you so much!