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 trialMatt Alston
5,549 PointsI can't figure out what's wrong with my code - I'm getting the error "switch statement must be exhaustive"
But it's an enum and I address both cases... could this be some other error? The code seems right to me...
import Foundation
enum UIBarButtonStyle {
case Done
case Plain
case Bordered
}
class UIBarButtonItem {
var title: String?
let style: UIBarButtonStyle
var target: AnyObject?
var action: Selector
init(title: String?, style: UIBarButtonStyle, target: AnyObject?, action: Selector) {
self.title = title
self.style = style
self.target = target
self.action = action
}
}
enum Button {
case Done(String)
case Edit(String)
func toUIBarButtonItem() -> UIBarButtonItem {
switch self {
case .Done("String"): return UIBarButtonItem(title: "String", style: UIBarButtonStyle.Done, target: nil, action: nil)
case .Edit("String"): return UIBarButtonItem(title: "String", style: UIBarButtonStyle.Plain, target: nil, action: nil)
}
}
}
let done = Button.Done("Done")
let doneButton = Button.toUIBarButtonItem(done)
4 Answers
Florian Laplanche
4,476 Pointsfunc toUIBarButtonItem() -> UIBarButtonItem {
switch self{
case Button.Done: return UIBarButtonItem(title: "Done", style: .Done, target: nil , action: nil)
case Button.Edit: return UIBarButtonItem(title: "Edit", style: .Plain, target: nil , action: nil)
}
}
}
let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()
this is what i used and it worked
Matt Alston
5,549 PointsThat worked - thank you!
Digvijay Jaiswal
5,565 PointsFlorian Laplanche can you please explain why we are using switch self? Ill appreciate that
Florian Laplanche
4,476 PointsThe use of the word self is because we are trying to use the enum cases in the enum itself. It lets the complier know we are switching on every case in the Button enum. Had we initialized a instance outside the enum and tried to create a switch, we wouldn't need to use the keyword because we are outside the enum. Did the make sense or would you need forth clarification?
Digvijay Jaiswal
5,565 PointsFlorian Laplanche I understood the first half of what you mentioned. I didn't understand the part where you said we wouldn't need to use a keyword because........can you explain that please?
Florian Laplanche
4,476 PointsAny time you reference a class, enum, or struct within itself, you have to use the keyword self. Any other time you don't have to worry about using it
Digvijay Jaiswal
5,565 PointsGot it now! Thanks so much Florian Laplanche !!
Florian Laplanche
4,476 Pointsanytime.
Florian Laplanche
4,476 PointsFlorian Laplanche
4,476 PointsYou don't need the ("String") for either case declaration