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 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()