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

Rich Valiant
Rich Valiant
2,417 Points

Calling an enum method on a constant with an enum value?

I was told to create a method on the enum Button which returned an instance of the UIBarButton class on the buttons.swift file. Then I was told to call that method on the constant created in the last lesson which is the let done = Button.Done("Done").

I did so, but I am getting the error message from the compiler that enums cannot contain values and refers me back to the let done = Button.Done("Done").

It passed through the first lesson, but now on this task in the challenge It's not. I added the method and then I called it on the done constant and added to a new constant called doneButton. I'm not sure what I'm doing wrong here.

Any help is vastly appreciated here!

Thanks,

Rich

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)

    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 doneButton = done.toUIBarButtonItem()

1 Answer

Aaron Kaye
Aaron Kaye
10,948 Points

I literally just spent like 20 minutes on the same issue lol. You forgot a closing curly brace to close the switch statement