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

Sean Lafferty
Sean Lafferty
3,029 Points

HELP PLEASE! I have no idea why this is failing!

Hi Guys, I am doing everything the challenge is asking but it still seems to fail. Its working fine in xcode and i dont understand!

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 self {
        case .Done(let done): return UIBarButtonItem(title: done, style: UIBarButtonStyle.Done, target: nil, action: nil)
        case .Edit(let edit): return UIBarButtonItem(title: edit, style: UIBarButtonStyle.Plain, target: nil, action: nil)
let done = Button.Done("Done")

let button = done.toUIBarButtonItem()

1 Answer

Charles Kenney
Charles Kenney
15,604 Points


There are a couple of things wrong with your code. In this challenge you are instructed to make an enum method, where you are to switch on the enum value itself and return it's respective UIBarButtonItem instance (it did not ask you to reimplement the UIKit framework's UIBarButtonItem class). To do this, you would create a method on the original BarButton enum with a return type of UIBarButtonItem.

// Example of UIBarButtonItem instance
// let someButton = UIBarButtonItem(title: "A Title", style: .plain, target: nil, action: nil)

enum BarButton {
    case done(title: String)
    case edit(title: String)
    func button() -> UIBarButtonItem {
      // return bar button item instance
      switch (self) {
      case .done(let title):
        return UIBarButtonItem(title: title, style: .done, target: nil, action: nil)
      case .edit(let title):
        return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)

// init BarButton
let done = BarButton.done(title: "Save")
// return BarButton's button instance
let button = done.button()

I hope this helps,