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

Julio Miranda
Julio Miranda
8,322 Points

Not fun!! Why is not working??

In the editor you've been provided with two files buttons.swift that contains some source code for you to use and enums.swift where you will be writing code.

Let's start simple. To a constant named done assign an enum value of type Button with the member Done. This member takes an associated value; assign it the string "Done".

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 str): return UIBarButtonItem(title: str, style: .Done, target: nil, action: nil)
    case .Edit(let str): return UIBarButtonItem(title: str, style: .Plain, target: nil, action: nil)
    }
  }
}

let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()
enums.swift
enum Button {
    case Done(String)
    case Edit(String)
}

1 Answer

thomas lotocki
thomas lotocki
3,230 Points

Hi Miranda,

I'm sure you already figured that out. Since I started with treehouse I learned one thing - google is your friend! ;-) Here is what I got while googling for your answer: http://cutting.io/posts/the-power-of-swift-enums/

I suppose one of the problems is with the way you declare the enums in enums.swift:

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

I think it should be declared as follows:

enum Button: String {
    case Done
    case Edit
}

Hope it makes sense.