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

Chase Maddox
PLUS
Chase Maddox
Courses Plus Student 5,461 Points

Confused on Enum Challenge Directions and Enum Syntax

Hi,

I'm having a tough time understanding exactly what this Code Challenge is asking me to do, and then because it's also my first time working with enums I'm still unfamiliar with the syntax and way to use enums properly. What am I not understanding in the directions and also in my implementation of enums?

Thanks, Chase

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, let style, let target, let action): 
          return UIBarButtonItem(title: title, style: UIBarButtonStyle.done, target: nil, action: nil) 

       case .edit(let title, let style, let target, let action):
          return UIBarButtonItem(title: title, style: UIBarButtonStyle.plain, target: nil, action: nil)

      }
    }
}

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

let button = done.button()

2 Answers

Garrett Votaw
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Garrett Votaw
iOS Development Techdegree Graduate 15,223 Points

Hey Chase,

You are on the right track. There are a couple problems with your code. Right now you are correctly binding the associated value of title to the correct enum member. However, you are also binding style, target, and action to the enum member but they aren't associated values.

// 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()
Chase Maddox
Chase Maddox
Courses Plus Student 5,461 Points

Ahh ok. Thanks Garrett, I really appreciate the help.

Gary Nguyen
Gary Nguyen
921 Points

Could the last two lines be condensed to

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

?