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

Enumerations and Optionals Swift 3.0 Enumeration Code Challenge Task 2 of 2

I have been on this prompt for hours. First off, no "initialization" example was listed (this being the Swift 3.0 version of the course, maybe they left it out?)

I was only able to pull what the UIBarButtonStyle class was from old posts about the Swift 2.0 version of the problem. Their solutions were not working for this question either.

Please tell me where I am going wrong?

buttons.swift
enum BarButton {
    case done(title: String)
    case edit(title: String)

    func button() -> UIBarButtonItem {
        switch self {
        case .done(let doneValue):
            return UIBarButtonItem(title: doneValue, style: UIBarButtonStyle.Done, target: nil, action: nil)
        case .edit(let editValue):
            return UIBarButtonItem(title: editValue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
        }
    }
}
let done = BarButton.done(title: "Save")
let button = done.button()

1 Answer

Solved it:

For onlookers, make sure you ignore some of the error messages when debugging. The error messages will tell you to name your constant doneButton

enum BarButton {
    case done(title: String)
    case edit(title: String)

    func button() -> UIBarButtonItem {
        switch self {
        case .done(let title):
            return UIBarButtonItem(title: title, style: .done, 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 button = done.button()