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

enum methods objective, please help me with this task

https://teamtreehouse.com/library/swift-20-enumerations-and-optionals/introduction-to-enumerations/enum-methods

link to the question as it is too long to post on here

i have tried alot of code but i cannot pass this question

Steven Parker
Steven Parker
243,095 Points

Show something you tried.

It's usually helpful to post the code from your best good-faith attempt, and folks can then make suggestions to help you make it work.

import Foundation

enum UIBarButtonStyle { case Done case Plain case Bordered }

class UIBarButtonItem {

var title: String?
let style: UIBarButtonStyle
var target: AnyObject?
var action: Selector

init(title: String?, style: UIBarButtonStyle, target: AnyObject?, action: Selector) {
    self.title = title
    self.style = style
    self.target = target
    self.action = action
}

}

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

func toUIBarButtonItem() -> UIBarButtonItem {
    switch Button {
    case Button.Done("Done"): return UIBarButtonItem(title: "Done", style: Done, target: nil, action: nil)
    case Button.Edit("Edit"): return UIBarButtonItem(title: "Edit", style: Plain, target: nil, action: nil)
    }
}

}

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

1 Answer

You're on the right track. In your switch statement you wouldn't want to switch on Button because you're testing for the case inside of the Button enum, not the enum itself. Lastly, the endings of these two lines of code are not necessary:

case: Button.Done("Done")
case: Button.Edit("Edit")

You don't actually need those two strings inside of the parenthesis. Other than that it looks good. This is how I did it:

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

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

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

I hope this can help you out a bit.

Thank you very much for your code it has helped me