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

Please I Can't Solve The Last Example in The Enumerations And optionals

Hi Team Tree House

please anyone can help me about solve the last issue in example last videos from section one in enumerations and optionals which have 2 task

thanks very much

3 Answers

Berry Loeffen
Berry Loeffen
4,303 Points
enum Button {
    case Done(String)
    case Edit(String)

    func toUIBarButtonItem() -> UIBarButtonItem {
        switch self {
        case .Done: return UIBarButtonItem(title: "Done", style: .Done, target: nil, action: nil)
        //.Done is from UIBarButtonStyle
        case .Edit: return UIBarButtonItem(title: "Edit", style: .Plain, target: nil, action: nil) 
        // .Plain is from UIBarButtonStyle
        }
    }
}
let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()

This worked for me.

Matte Edens
Matte Edens
9,788 Points

This should work too and even assigns the title value that was passed in when creating the done constant in the previous step. However, the puzzling thing is that this answer is not accepted. The preview linter doesn't report any errors but this answer doesn't work, and the error modal even says to see the preview area for any errors. It finally works once you create a separate done constant and then call toUIBarButtonItem() on it. Weird. To my knowledge chaining like I did below isn't wrong.

(e)

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()
Berry Loeffen
Berry Loeffen
4,303 Points

This indeed works as well but for as far is I can see in stead of:

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

write:

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

As said in the first task of this question, first assign a constant called done to an instance of Button.

Your answer is indeed correct as well.

James Estrada
seal-mask
.a{fill-rule:evenodd;}techdegree
James Estrada
Full Stack JavaScript Techdegree Student 25,866 Points

This is how I solved it using the concepts learned from the "Introduction to Enumerations" series.

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

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