Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!