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

Bruce Röttgers
Bruce Röttgers
18,211 Points

Need help with the method at exercise 2

Can't figure out what I have really to do in the method.

Error output (from Exercise): swift_lint.swift:51:15: error: enum case 'done' not found in type 'BarButton.Type' case .done: return UiBarButtonStyle.done ^ swift_lint.swift:51:28: error: use of unresolved identifier 'UiBarButtonStyle' case .done: return UiBarButtonStyle.done ^~~~~~~~~~~~~~~~ swift_lint.swift:27:7: note: did you mean 'UIBarButtonItem'? class UIBarButtonItem { ^ swift_lint.swift:22:6: note: did you mean 'UIBarButtonItemStyle'? enum UIBarButtonItemStyle : Int { ^ swift_lint.swift:52:15: error: enum case 'edit' not found in type 'BarButton.Type' case .edit: return UIBarButtonStyle.plain ^ swift_lint.swift:52:28: error: use of unresolved identifier 'UIBarButtonStyle' case .edit: return UIBarButtonStyle.plain ^~~~~~~~~~~~~~~~ swift_lint.swift:27:7: note: did you mean 'UIBarButtonItem'? class UIBarButtonItem { ^ swift_lint.swift:22:6: note: did you mean 'UIBarButtonItemStyle'? enum UIBarButtonItemStyle : Int { ^

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 BarButton.self {
        case .done: return UiBarButtonStyle.done
        case .edit: return UIBarButtonStyle.plain
      }
    }
}

let done = BarButton.done(title: "Save")
let button = done.button()

1 Answer

Jeff McDivitt
Jeff McDivitt
23,970 Points
enum BarButton {
    case done(title: String)
    case edit(title: String)

    func button() -> UIBarButtonItem{
        switch self {
        case .done(let title):
            return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
        case .edit(let title):
            return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
    }
}

}
let done = BarButton.done(title: "Save")
let doneButton = done.button()