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

Sayuri Katz
Sayuri Katz
6,148 Points

Next, add a method to the enum named button that returns an instance of the class UIBarButtonItem configured properly.

Need help!

`

1 Answer

Dan Lindsay
Dan Lindsay
39,611 Points

Hey Sayuri,

Let me see if I can help on this one. First, the challenge asks you to create a method named button, and you have named yours BarButtonItem. So let’s first create the method stubbing our enum, like so:

func button()  -> UIBarButtonItem {

}

For the next part, we need to create a switch statement on our two cases, done and edit. Since these are inside our enum, we need to switch on self. This will look like this:

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

See that in our switch statement, we just use the associated values(.done, .edit) so we don’t have to worry about the (title: String) bit. Now that we have our method, we just need to call it on our done constant and store that value in a new constant called button, like so:

let done = BarButton.done(title: Save) //this was from task 1
let button = done.button()

This should pass the challenge, once you delete the switch statement you declared outside the enum as well. Hope this helps you out!

Dan