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 Enumerations and Optionals in Swift Introduction to Enumerations Enum Methods

Ben Hendricks
seal-mask
.a{fill-rule:evenodd;}techdegree
Ben Hendricks
Python Web Development Techdegree Student 15,217 Points

Enum Methods challenge Swift 3.0 giving me troubles

Hi all, I'm probably glossing over something silly, but I can't seem to get the right answer here. I don't have any errors in xcode compiler, but upon checking work it just keeps saying "Your code could not be compiled" despite there being no errors in the editor. Any ideas what may be amiss with the below?

enum BarButton { case done(title: String) case edit(title: String)

func MakeMeAbutton() -> UIBarButtonItem {
    switch self {
    case .done(let doneValue):
        return UIBarButtonItem(title: doneValue, style: .done, target: nil, action: nil)
    case .edit(let editValue):
        return UIBarButtonItem(title: editValue, style: .plain, target: nil, action: nil)
    }
}

}

let button = BarButton.done(title: "Done") let doneButton = button.MakeMeAbutton()

buttons.swift
// Example of UIBarButtonItem instance
// let someButton = UIBarButtonItem(title: "A Title", style: .plain, target: nil, action: nil)
enum BarButton {
    case done(title: String)
    case edit(title: String)

    func MakeMeAbutton() -> UIBarButtonItem {
        switch self {
        case .done(let doneValue):
            return UIBarButtonItem(title: doneValue, style: .done, target: nil, action: nil)
        case .edit(let editValue):
            return UIBarButtonItem(title: editValue, style: .plain, target: nil, action: nil)
        }
    }
}

let button = BarButton.done(title: "Done")
let doneButton = button.MakeMeAbutton()

1 Answer

Thomas Dobson
Thomas Dobson
7,511 Points

Hey Ben,

I can confirm your code was compiling within Xcode for me at least. I was able to get your code to compile and function within the code challenge just by adjusting your naming conventions to the requests of the scenario. My guess is the way the code challenge is checking your solution, played into your compiler errors from the various name discrepancies. I cant speak to how the treehouse code challenge compiler actually works though.

Ultimately something like this:

// Example of UIBarButtonItem instance
// let someButton = UIBarButtonItem(title: "A Title", style: .plain, target: nil, action: nil)
enum BarButton {
    case done(title: String)
    case edit(title: String)

    func button() -> UIBarButtonItem {
        switch self {
        case .done(let doneValue):
            return UIBarButtonItem(title: doneValue, style: .done, target: nil, action: nil)
        case .edit(let editValue):
            return UIBarButtonItem(title: editValue, style: .plain, target: nil, action: nil)
        }
    }
}

let done = BarButton.done(title: "Save")
let button = done.button()

I hope this helps.

Ben Hendricks
seal-mask
.a{fill-rule:evenodd;}techdegree
Ben Hendricks
Python Web Development Techdegree Student 15,217 Points

THANK YOU SO MUCH! I had been staring at this for far too long and had forgotten to change all my names back. That seemed to do the trick.