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

Craig Weston
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Craig Weston
Front End Web Development Techdegree Graduate 32,840 Points

Hi, I'm stuck on this Challenge. To the button enum, add a method named toUIBarButtonItem that returns an instance of

This is the Challenge:

Challenge Task 2 of 2

To the button enum, add a method named toUIBarButtonItem that returns an instance of UIBarButton item configured properly.

In the buttons.swift file there is a basic implementation of UIBarButtonItem. You can create buttons with three different styles and titles.

Using the associated values as titles for the button, return a button with style UIBarButtonStyle.Done for the Done member of the Button enum. Similarly for the Edit member, return a UIBarButtonItem instance with the style set to UIBarButtonStyle.Plain.

In both cases you can pass nil for target and action. Once you have a method, call it on the value we created in the previous task and assign it to a constant named doneButton.

Here is what I have put:

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

func toUIBarButtonItem() -> UIBarButtonItem {
    switch self {
    case .Done("Done"):

    return UIBarButtonItem(title: "Done", style: UIBarButtonStyle.Done, target: nil, action: nil)

    case .Edit("Edit"):
    return UIBarButtonItem(title: "Edit", style: UIBarButtonStyle.Plain, target: nil, action: nil)

    }
}

}

let doneButton = Button.Done

This is the error:

swift_lint.swift:35:9: error: switch must be exhaustive, consider adding a default clause } ^ swift_lint.swift:35:9: error: switch must be exhaustive, consider adding a default clause } ^

buttons.swift
import Foundation

enum UIBarButtonStyle {
    case Done
    case Plain
    case Bordered
}

class UIBarButtonItem {

    var title: String?
    let style: UIBarButtonStyle
    var target: AnyObject?
    var action: Selector

    init(title: String?, style: UIBarButtonStyle, target: AnyObject?, action: Selector) {
        self.title = title
        self.style = style
        self.target = target
        self.action = action
    }
}
enums.swift
enum Button {
    case Done(String)
    case Edit(String)

    func toUIBarButtonItem() -> UIBarButtonItem {
        switch self {
          case .Done("Done"):
          return UIBarButtonItem(title: "Done", style: UIBarButtonStyle.Done, target: nil, action: nil)

          case .Edit("Edit"):
          return UIBarButtonItem(title: "Edit", style: UIBarButtonStyle.Plain, target: nil, action: nil)
        }
    }
}

let doneButton = Button.Done

7 Answers

Hi everyone. I had a real hard time with this one in the Swift 3.0 one as well. But here is the answer I got for it:

// 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: 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()
Dan Hickey
Dan Hickey
3,016 Points

Did this pass the challenge?

Evans Attafuah
Evans Attafuah
18,277 Points
enum BarButton {
    case done(title: String)
    case edit(title: String)

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

let done = BarButton.done(title: "Save")
let button = done.button()
Ethan Neff
Ethan Neff
27,058 Points

try this:

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

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

let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()

you're missing the part where you are passing the associated value through the switch statement .Done(let str)

Joe Hill's above answer is correct (thanks, Joe!). You can alternatively move the constant declaration keyword (i.e., "let") outside of the parentheses (code shown below). For more details, check out Apple's Swift documentation on Enums (particularly focus on Associated Values and Recursive Enumerations sections).

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

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

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

Yes it did. I had the titles mixed up. I updated my answer above so it should be correct now. My cases should have just said:

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)
Laith Mohammad
Laith Mohammad
6,569 Points

This is a pointless, unclear example, there's no need to use UI component in explaining a programming language, this is a swift course , not an xcode IOS development course.

Dear treehouse team, please use a straight forward example instead.

I'm curious as to why after .done you guys put (let title). can someone explain this for me please?

For anyone curious:

We do this — declare a local constant, e.g., .done(let title) or let .done(title) — because in order to use an enum member's associated value in the switch statement, you must extract it by creating a local constant to work with. We want to be able to pass this value through and utilize it in the return statement, so first we must name it ("Bastian, please; save us!") :sparkles::wink: so that we can reference it (in this case, passing its value through for the title).

For more, check out the Swift documentation on Enums, particularly the Associated Values section.

Abdoulaye Diallo
Abdoulaye Diallo
15,863 Points

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()