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

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,936 Points

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