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

s t
s t
2,699 Points

enums and optionals objective 2 - please provide correct answer

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 doneButton = UIBarButtonItem(title: "title", style: .Done, target: nil, action: nil)

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(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 doneButton = UIBarButtonItem(title: "title", style: .Done, target: nil, action: nil)

3 Answers

Arman Arutyunov
Arman Arutyunov
21,900 Points

Look, your done constant is just an enum instance with a "Save" title as a passed parameter. What you are asked to do is just to call your function you implemented inside the enum. But as it is a function that belongs to the enum you have to call it from the enum instance. And done constant is your enum instance, so the function will configure everything smoothly with that instance and return a proper UIBarButtonItem

let done = BarButton.done(title: "Save")
let button = done.button()
s t
s t
2,699 Points

Hi, I appreciate all your help and apologize for minusing your answer, I actually didn't even realize what that was for so was clicking on it unwittingly, I have changed it back. The code is still not passing. Can you please write out the full code so that I can copy it exactly and paste it so I can pass the objective, I will then be able to then understand exactly where I am going wrong. No explanation need, just the exact code to pass. Thanks!!

Arman Arutyunov
Arman Arutyunov
21,900 Points

I didn't pass the whole snippet because everything except for the last line of your code was correct. That should work fine.

// 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(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()