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

Don't understand what to do for the challenge.

Really struggling with the second part of this challenge. I'm just not sure what to do/where to start. If I could see the solution code, I could understand it, and I would be so greatly appreciative if someone could help me out.

Thanks.

1 Answer

Nathan Tallack
Nathan Tallack
22,159 Points

Yeah, this one was a tough one.

Consider my code example below. The comments inline will help you understand.

// This enum was defined for us with these two cases.
enum Button {
    case Done(String)
    case Edit(String)

    // In the second step we create this method so that we can call it against the done object.
    func toUIBarButtonItem() -> UIBarButtonItem {  // This method will return a UIBarButtonItem
        switch self {  // We are switching on our own object cases.
        case .Done(let value):  // Here we are passing the assigned value in as the value variable.
            return UIBarButtonItem(  // This is the return item for our function.
                title: value,  // This is the string we set when we created our done object.
                style: UIBarButtonStyle.Done,  // This case uses the Done button style.
                target: nil,
                action: nil)
        case .Edit(let value):  // Again we are passing the assigned value in as the value variable.
            return UIBarButtonItem(  // Again we will return this item for the Edit case.
                title: value,  // Using the value string passed in to set as the title.
                style: UIBarButtonStyle.Plain,  // This case would use the Plain button style.
                target: nil,
                action: nil)
        }
    }
}

let done = Button.Done("Done")  // In the first step we create this object.
let doneButton = done.toUIBarButtonItem()  // In the second step we call this method on the object.

It is quite a complex method. Read through it and understand how it works with the Class used in this challenge.

Feel free to ask any questions. :)

Thanks so much! The commentary was so helpful. I hope you had a wonderful New Year. :)