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

Arius Eich
Arius Eich
2,769 Points

Help please

I have been stuck on this challenge for a while. Please help. Thank you!

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

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


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

1 Answer

Everton Carneiro
Everton Carneiro
15,994 Points

Hello mate. You need to pay attention in the instructions of the challenge. There's a couple of errors in your code:

  • 1 : You don't need to specify the title in the switch statement, just the case itself. The correct syntax is:
        switch self {
        case .done:
           //code here
        case .edit:
           //code here
        }
  • 2 : In the challenge you are asked to return an instance of UIBarbutton as the comments they've provide, your syntax also is incorrect in this part:
   return .done(title: "/(title)", style: .plain, target: nil, action: nil)
  • 3: To call the method, you need to use the instance of BarButton that you create, with should be:
let done = BarButton.done(title: "Save")
let button = done.button()

instead of:

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

The compiler doesn't recognize button.done because you've never created an instance called button, you create an instance called done.

The right final version look like this:

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

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

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

I hope that helps.