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 Methods on Enumerations

boris said
boris said
3,607 Points

Second part of code challenge saying I need to assign result to doneButton constant; I have done that, error continues

Here is my code:

Thank you in advanced for your help and support enum Button { case Done(String) case Edit(String)

func toUIBarButtonItem() -> 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 doneButton = Button.Done("Done").toUIBarButtonItem()

2 Answers

You were very close to the correct answer. The only issue that I can see with your code is that the last parts that you added to these lines of code were not necessary:

case .Done(let title):
case .Edit(let title):

Other than that I believe your code would have compiled nicely. This is how I did it:

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

I hope this helps a bit.

boris said
boris said
3,607 Points

Thanks it helped a lot.