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

Getting a code compile error with no output - compiles fine in local playground.

I'm trying to understand what I'm doing wrong that isn't allowing me to pass this exercise. It says my code doesn't compile, but I'm not getting any output from the compiler. When I write the code in my local playground it works fine:

// enums.swift file ā€‹ enum Button { case Done(title: String) case Edit(title: String)

func toUIBarButtonItem() -> UIBarButtonItem {
    switch self {
    case .Done(let titleValue): return UIBarButtonItem(title: titleValue, style: UIBarButtonStyle.Done, target: nil, action: nil)
    case .Edit(let titleValue): return UIBarButtonItem(title: titleValue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
    }
}

} ā€‹ let doneButton = Button.Done(title: "Done").toUIBarButtonItem()

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
import buttons.swift

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

     func toUIBarButtonItem() -> UIBarButtonItem {
        switch self {
        case .Done(let titleValue): return UIBarButtonItem(title: titleValue, style: UIBarButtonStyle.Done, target: nil, action: nil)
        case .Edit(let titleValue): return UIBarButtonItem(title: titleValue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
        }
    }
}

let doneButton = Button.Done("Done").toUIBarButtonItem()

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey David Mitchell,

Your code is a valid solution. However, this is not what the code challenge is checking for. As a general rule, don't edit the code used to pass the previous task unless you are asked to do so.

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

     func toUIBarButtonItem() -> UIBarButtonItem {
        switch self {
        case .Done(let titleValue): return UIBarButtonItem(title: titleValue, style: UIBarButtonStyle.Done, target: nil, action: nil)
        case .Edit(let titleValue): return UIBarButtonItem(title: titleValue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
        }
    }
}

/* You need to use your code from the previous task
and call toUIBarButtonItem() on the done constant */
let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()

Good Luck!