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

Chigozie Ofodike
Chigozie Ofodike
11,197 Points

Hi there. Can I get some help?

It's saying i didnt have a default case even though i used an enum. Also I assigned done.button() to the constant doneButton yet it's saying it hasnt been done. What am I doing wrong? Also for the enum i did this " default: return UIBarButtonItem(title: "edit", style: .plain, target: nil, action: nil) } " to get rid of the error code but shouldnt it be automatic?

buttons.swift
// Example of UIBarButtonItem instance
// let someButton = UIBarButtonItem(title: "A Title", style: .plain, target: nil, action: nil)

enum BarButton {
    case done(title: String)
    case edit(title: String)
    func button() -> UIBarButtonItem {
        switch self {
        case .done(title: "done"): return UIBarButtonItem(title: "done", style: .done, target: nil, action: nil)
        case .edit(title: "edit"): return UIBarButtonItem(title: "edit", style: .plain, target: nil, action: nil)
        }
    }
}
let done = BarButton.done(title: "Save")
let doneButton = done.button()

2 Answers

Xavier D
PLUS
Xavier D
Courses Plus Student 5,840 Points

Hey Chigozie,

Everything looks right except the cases of your switch--you need to fix that. The compiler is asking for a default clause because it think that you didn't include all the cases in self--and you didn't.

.done isn't the same as .done(title: "done") .edit isn't the same as .edit(title: "edit")

The compilers believes that you weren't exhaustive because you left out a couple of cases.