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

Maarten De Kruif
Maarten De Kruif
7,314 Points

how to add the method button

I'm stuck with the method, has anyone has a suggestion. Thanks in advance.

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 UIBarButtonStyle.done()
      case .edit(let title): return UIBarButtonStyle.plain()
      }
    }
}

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

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Maarten,

You're on the right track and most of the code is correct. The problem lies inside of the switch statement.

First, you are returning UIBarButtonStyle when it should be UIBarButtonItem. I can see how the instructions were misinterpreted, and how you arrived at this conclusion. But, you will need to use UIBarButtonItem with the appropriate style declared inside.

This bring us to the second issue. There is no initializer. The challenge wants

an instance of the class UIBarButtonItem configured properly. An example of how to create an instance for this task is shown in the comments below. Use the same initializer.

If you look at second line in the pre-loaded code, the comment gives you the format for the initialization.

Give it another shot with this in mind. If you are still stuck, just leave a comment here.

Keep Coding! :) :dizzy:

Maarten De Kruif
Maarten De Kruif
7,314 Points

Jason,

Thanks for the help. Still stuck though. Do i have to make my own class UIBarButonItem?

I tried the following. But still stuck unfortunately.

Greetz, Maarten

class UIBarButtonItem { var title: String var style: String var target: String! var action: String!

init(title: String, style: String, target: String!, action: String!) { self.title = title self.style = style self.target = target self.action = action } }

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

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

}

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

Maarten De Kruif
Maarten De Kruif
7,314 Points

Jason,

Thanks for the help. Still stuck though. Do i have to make my own class UIBarButonItem?

I tried the following. But still stuck unfortunately.

Greetz, Maarten


class UIBarButtonItem {
  var title: String
  var style: String
  var target: String!
  var action: String!

  init(title: String, style: String, target: String!, action: String!) { 
   self.title = title
   self.style = style
   self.target = target
   self.action = action
   }
}

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

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

let done = BarButton.done(title: "Save")
let button = BarButton.done.button()
Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

No, you don't have to make your own class. In fact, your original code you posted was pretty much correct, except for the cases inside of the switch statement. If you look back at my first response, what it means is that you just needed to change UIBarButtonStyle to UIBarButtonItem and add the initializers to that line.

If you want to, you can have a look at a previous response of mine, but it does actually give you the correct answers... so "Spoiler Alert"!

Have a look at the correct answer there (makes sure to read my response as to the one error in that posted code). Compare it to your original answer as well as my original response here. That should help you figure out the how and the why for the right code. :)

Hope this clears it up for you. :smiley:

:dizzy: