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 Enumerations and Optionals Introduction to Enumerations Enum Methods

Matt Alston
Matt Alston
5,549 Points

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

buttons.swift
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
    }
}
enums.swift
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)
Florian Laplanche
Florian Laplanche
4,476 Points

You don't need the ("String") for either case declaration

4 Answers

Florian Laplanche
Florian Laplanche
4,476 Points
func 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
Matt Alston
5,549 Points

That worked - thank you!

Digvijay Jaiswal
Digvijay Jaiswal
5,565 Points

Florian Laplanche can you please explain why we are using switch self? Ill appreciate that

Florian Laplanche
Florian Laplanche
4,476 Points

The 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
Digvijay Jaiswal
5,565 Points

Florian 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
Florian Laplanche
4,476 Points

Any 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