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

Matt Pallotta
Matt Pallotta
5,333 Points

Having trouble with enum method code challenge

I compiled the attached code in XCode and it is working fine, however, the Treehouse compiler is saying there is an error. There is not an error listed on the Preview page. Could you provide some guidance on how to fix my code?

Additionally, I noticed what I think is an error in the problem statement. In the first paragraph: "instance of UIBarButton item" should read "instance of UIBarButtonItem"

Thanks for the help

Nathan Tallack
Nathan Tallack
22,160 Points

Your code did not attach. Could you paste it in?

Matt Pallotta
Matt Pallotta
5,333 Points

This is what I put into the enums.swift file. I left the buttons.swift file as is.

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

func toUIBarButtonItem() -> UIBarButtonItem{

    switch self{
    case .Done(let dvalue): return UIBarButtonItem(title: dvalue, style: UIBarButtonStyle.Done, target: nil, action: nil)
    case .Edit(let evalue):  return UIBarButtonItem(title: evalue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
    }
}

}

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

1 Answer

Nathan Tallack
Nathan Tallack
22,160 Points

You were VERY close. :)

You needed to call toUIBarButtonItem on the done object you made in the first step. Semantics of the challenge I am afraid. ;)

Formatted your code a little so that it pastes in better. :)

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

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

let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()
Matt Pallotta
Matt Pallotta
5,333 Points

Thanks for the help Nathan.

Thank you so much! The challenge gives absolutely no idea about what to do. I got scared seeing all those optionals.