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

shane reichefeld
PLUS
shane reichefeld
Courses Plus Student 2,232 Points

What do i do?

Enum Help?

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

2 Answers

The challenge asks for a function. As the videos have pointed out, functions start with the keyword func. Then you give the function a name, paretheses (with parameters, if there are any), and a return type (if it's other than Void). Inside the function you need a switch statement that switches on the various cases of the enum. You use self to self to refer to the enum, and you write a case statement for each of the enum's cases. In the cases' return statements you need to use the associated values as the button titles. Something along these lines. They asked for only two of the cases, but I've put the third one in just for completeness. The editor doesn't mind.

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

    func toUIBarButtonItem() -> UIBarButtonItem {
        switch self {
        case .Done(let doneValue):
            return UIBarButtonItem(title: doneValue, style: UIBarButtonStyle.Done, target: nil, action: nil)
        case .Edit(let editValue):
            return UIBarButtonItem(title: editValue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
        case .Bordered(let borderValue):  //not in challenge
            return UIBarButtonItem(title: borderValue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
        }
    }
}
let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()
shane reichefeld
shane reichefeld
Courses Plus Student 2,232 Points

Im still confused on how they want the answer i cant seem to get it correct

shane, I'm puzzled? I'm not sure what you mean by "get it correct"? If you mean that you cannot pass the challenge, all you need to do is copy my enum and paste it over the enum that is in the enums.swift file.

Or if you don't see enums.swift, note that the challenge opens with buttons.swift showing. You need to click the tab that says enums.swift to open it first.

Or?