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

Francisco Quintero
PLUS
Francisco Quintero
Courses Plus Student 1,586 Points

How do i complete this code challenge?

I have been stuck with this code challenge for about a week, am not a native english speaker and i dont understand what exactly am getting wrong, I would really appreciate the 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)
    func button () -> UIBarButtonItem {
      switch self {
       case .done(let title): return UIBarButtonItem(title: "A title", style: UIBarButtonStyle.done , target: nil, action: nil)
       case .edit(let title): return UIBarButtonItem(tittle: "A title", style: UIBarButtonStyle.plain, target: nil, action: nil)
      }
    }
}

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

2 Answers

Francisco Quintero
PLUS
Francisco Quintero
Courses Plus Student 1,586 Points

Here is the answer, hope this helps someone just as i did!

          <p>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)
      }   
    }
}

let done = BarButton.done(title: "Save")
let button = done.button()</p>
          ```

Hi!

Three things: 1) The title is not "A title", but the constant title you have declared 2) Look up in the comment how to properly set the style 3) Call the method button() on the last line

And it should work :)

Francisco Quintero
Francisco Quintero
Courses Plus Student 1,586 Points

Hi! I corrected the first and third suggestions, but I am not clear with what you mean in the second suggestion, I am not clear how to guide me from the example.