Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Kuang-Wen, Huang
Kuang-Wen, Huang
2,525 Points

Problem with Enum task 2

Hello everyone, I got some problem with Enum in task 2, which says / *To the button enum, add a method named toUIBarButtonItem that returns an instance of UIBarButtonItem configured properly.

In the buttons.swift file there is a basic implementation of UIBarButtonItem. You can create buttons with three different styles and titles.

Using the associated values as titles for the button, return a button with style UIBarButtonStyle.Done for the Done member of the Button enum. Similarly for the Edit member, return a UIBarButtonItem instance with the style set to UIBarButtonStyle.Plain.

In both cases you can pass nil for target and action. Once you have a method, call it on the value we created in the previous task and assign it to a constant named doneButton. /*

And here is my question, when I type case .Done: return UIBarButtonItem(title: "Done", style: .Done, target: nil, action: nil) the compiler says I should put a String after .Done, ex. style: .Done("someString"). But when I copy the syntax from the playground to treehouse is wrong. And i found if I delete ("somestring") just like the syntax I post it works on the treehouse but error in the playground, what is the different?

Thanks a lot!

//Which works in treehouse but not the playground.

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)
    case .Edit: return UIBarButtonItem(title: "Edit", style: .Plain, target: nil, action: nil) 
    }
}

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

1 Answer

Attila Secchin
Attila Secchin
2,420 Points

You can find an answer to your question on these:

https://teamtreehouse.com/community/my-code-seems-to-be-working-in-the-xcode-compiler-but-not-on-the-website-im-not-exactly-sure-why

and

https://teamtreehouse.com/community/i-am-not-understanding-how-to-do-this-task

But to answer you briefly, try this:

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

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

let done = Button.Done("Done")

let doneButton = done.toUIBarButtonItem()

ps: you need to pass the string to your enums (.Done and .Edit) and the "title" should be the same constant you pass to the string, and not the value "Done" or "Edit" - it actually depends on the input. If I create a button with the name "Chewie", it's title is not supposed to be "Done", even if I create a Done() button.