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

Help! I don't understand.

I really don't get this at all but I want to do it myself, so could someone explain it to me?

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
    }
}
enums.swift
enum Button {
    case Done(String)
    case Edit(String)
}
let done = Button.Done("Done")

1 Answer

Matthew Connolly
seal-mask
.a{fill-rule:evenodd;}techdegree
Matthew Connolly
iOS Development Techdegree Student 11,595 Points

Hi swiftdev,

It looks like you have the correct answer to Task #1. Are you having trouble with Task #2?

Steps:

  1. You need to add a method to Button that will create a UIBarButtonItem
  2. Look to the buttons.swift page to see what arguments you need to create a UIBarButtonItem; you will need a title and a button style (target and action will be nil according to the instructions)
  3. Depending on the enum Button created, use a switch statement to create the respective UIBarButtonItem
  4. Call the method and assign to a constant doneButton
enum Button {
    case Done(String)
    case Edit(String)

    func toUIBarButtonItem() -> UIBarButtonItem {
        switch self {
        case .Done(/* insert your code */): return UIBarButtonItem(/* insert your code */)
        case .Edit(/* insert your code */): return UIBarButtonItem(/* insert your code */)
        }
    }
}

let done = Button.Done("Done")

let doneButton = done.toUIBarButtonItem()