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

Brandon Lenz
Brandon Lenz
7,937 Points

Enumerations and Optionals Code Challenge

Please see code challenge for info.

I have written code that produces no compiler errors in Xcode yet treehouse assumes there are compiler errors (none are shown in preview window either)

When I use

import UIKit

treehouse throws a compiler error. When I don't Xcode throws a compiler error. Either way something is wrong here.

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 GetUIBarButtonItem() -> UIBarButtonItem {
        switch self { 
        case .done(let title): return UIBarButtonItem(title: title, style: .done, target: nil, action: nil)
        case .edit(let title): return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
        }
    }
}

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

1 Answer

Tassia Castro
seal-mask
.a{fill-rule:evenodd;}techdegree
Tassia Castro
iOS Development Techdegree Student 9,170 Points

Hey Brandon,

In the question they ask you to name the method as 'button'.

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

    func button() -> UIBarButtonItem { // I renamed the method to button.
        switch self { 
        case .done(let title): return UIBarButtonItem(title: title, style: .done, target: nil, action: nil)
        case .edit(let title): return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
    }
}
}
let done = BarButton.done(title: "Save")
let button = done.button() // Now I'm calling 'button'

Hope this helps =)