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

William Humphrey
William Humphrey
5,543 Points

whats missing in this?

I have made 3 errors I can't find please help.

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)
}
class UIBarButtonItem {
    var title: String
    let style: UIBarButtonItemStyle
    var target: nil
    var action: nil

    init (title: String, style: UIBarButtonItemStyle, target: nil, action: nil){

    self.title = title
    self.style = style
    self.target = target
    self.action = action

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

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

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

2 Answers

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

Hi William, there are a couple things missing and some stuff that didn't need to be written. You don't have to make a class for UIBarButtonItem and the enum you made for button is supposed to be a function inside the enum they provided for you. Anytime a problem refers to a method what they mean is a function inside of an object like an enum, struct, or class. So when you say to create a method named button what they mean is a specialized function in the enum they provided for you. The function you made at the bottom has the right idea its just missing one thing. Every time you write a function when you return a type you must return that type inside the function. When writing a switch statement this will be after you have written your case. I have the solution below.

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

    func button() -> 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)
        }
         // after we write the case we return the UIBarButtonItem and this satisfies the return type at the end of our function 

    }
}

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

Hope this helps

William Humphrey
William Humphrey
5,543 Points

Thank you that makes more sense now