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

Svein Bruce
Svein Bruce
7,162 Points

I don't really understand the task. What is UIBarButtonItem and where do I get it from?

What am I actually returning? Am i converting a BarButton enum to a usable button? I have not started on UI tracks yet, and have not seen the UIBarButtonItem class before.

My code does not compile either, but I fell I shouldn't try to fix it without understanding what I'm doing

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: return UIBarButtonStyle.done 
    case .edit: return UIBarButtonStyle.plain

    }

    }
}

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

1 Answer

Jari Koopman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jari Koopman
Python Web Development Techdegree Graduate 29,349 Points

Hi Svein,

For this example, it doesn't really matter what a UIBarButtonItem is and what it does. But if you'd like to read about it, here are the links to the docs. As far as the challenge is considered, what you're asked to do is create a function, button() for the enum, that returns a UIBarButtonItem instance. You can see how to create one in the comment in the code. Next to that, you have to set the style of the button dependant on your BarButton case. So, you could approach this something like this:

func button() -> UIBarButtonItem {
    switch self {
        case .done:
            //Return a UIBarButtonItem with `UIBarButtonStyle.done` style
        case .edit:
            //Return a UIBarButtonItem with `UIBarButtonStyle.plain` style
    }
}

I'll leave the exact implementation to you, but I think this should be a good nudge in the right direction!

Hope this helped,

Regards, Jari