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

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!

buttons.swift
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

Sean,

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,

Charles