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 Enumerations and Optionals in Swift Introduction to Enumerations Enum Methods

Vas Pon
Vas Pon
655 Points

can't understand the part with this styles?

I can't understand it, please help me!

buttons.swift
// Example of UIBarButtonItem instance
// let someButton = UIBarButtonItem(title: "A Title", style: .plain, target: nil, action: nil)
enum BarButton {
    case done(title: String, style: .plain, target: nil, action: nil)
    case edit(title: String, style: .plain, target: nil, action: nil)
    func button() -> UIBarButtonItem {
      switch self {
        case .done(let title, let style, let target, let action): return UIButtonItem(title, style: UIBarButtonStyle.done, target: nil, action: nil) 
        case .edit(let title, let style, let target, let action): return UIButtonItem(title, style: UIBarButtonStyle.plain, target: nil, action: nil)
      }
    }
}
let button = BarButton(title: "A Title", style: .plain, target: nil, action: nil).button

EDIT: Your title has been adjusted to remain appropriate and in compliance with our terms.

1 Answer

Jorge Solana
Jorge Solana
6,064 Points

Hey Vas!

You're getting confused with the example. You don't need to redefine the enum case statements, we only have to return that type of element in the function. Look at this:

enum BarButton {
    case done(title: String)
    case edit(title: String)
    func button() -> UIBarButtonItem {
        switch self {
        case .done(let title):
/*this is where the example comes in handy, 
and we don't need to change anything else but the "title" thing since we already got it*/
            return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
        case .edit(let title):
            //this is where the example comes in handy
            return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
        }
    }
}

let done = BarButton.done(title: "Save")
let button = done.button()

Hope it helps. Keep up the hard work!