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 Swift 2.0 Enumerations and Optionals Introduction to Enumerations Enum Methods

Jacob Herper
Jacob Herper
94,150 Points

I am stuck with the second task of the Enum Methods challenge.

Can anyone post their solution so I can figure out where I messed up? I'm stuck for a few hours now and it's getting really frustrating. Thanks!

3 Answers

Hi Jacob,

If you post the code that you've tried so far, we're able to point you in the right direction - and potentially help you learn more from the Challenge - than if we just give you the answer.

That said, this code will pass the Challenge:

enum Button {
    case Done(String)
    case Edit(String)

  func toUIBarButtonItem() -> UIBarButtonItem {
        switch self {
        case .Done: return UIBarButtonItem(title: "Done", style: UIBarButtonStyle.Done, target: nil, action: nil)
        case .Edit: return UIBarButtonItem(title: "edit", style: UIBarButtonStyle.Plain, target: nil, action: nil)
        }
    }
}

let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()

Hope that helps, feel free to ask questions about why this works if you have any!

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Thanks for the help Even. I was having a lot of issues. The first being I was in the wrong file!

Jacob Herper
Jacob Herper
94,150 Points

Thanks and sorry I posted this after I had closed the challenge.

No worries, happy to help!

Ryan Harp
PLUS
Ryan Harp
Courses Plus Student 1,763 Points

Here is the correct code

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