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 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!