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

Enumerations

Don't understand this clearly. Can anyone help?

Challenge Task 1 of 2

In the editor you've been provided with two files buttons.swift that contains some source code for you to use and enums.swift where you will be writing code.

Let's start simple. To a constant named done assign an enum value of type Button with the member Done. This member takes an associated value; assign it the string "Done".

/Users/mattconway/Desktop/Screen Shot 2016-03-07 at 3.22.50 PM.png

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

3 Answers

The following does a bit more than they ask for, but it will pass the challenge. Since there are 3 button cases, I thought it would be nice to have the function be able to return any of the three:

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

    func toUIBarButtonItem() -> UIBarButtonItem {
        switch self {
        case .Done(let doneValue):
            return UIBarButtonItem(title: doneValue, style: UIBarButtonStyle.Done, target: nil, action: nil)
        case .Edit(let editValue):
            return UIBarButtonItem(title: editValue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
        case .Bordered(let borderValue):  //not in challenge
            return UIBarButtonItem(title: borderValue, style: UIBarButtonStyle.Plain, target: nil, action: nil)
        }
    }
}

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

let bordered = Button.Bordered("Bordered")  //not in challenge
let borderedButton = bordered.toUIBarButtonItem()
codyl
codyl
4,704 Points

I've seen a few answers to this question that involve things that there is no way someone taking these classes would know. wtf is UIBarButtonItem and all of that? I'm sure we'll get to that sometime but if that was in any way the expected answer it would be pretty depressing for anyone like me that's just happens to know what was taught.

The answer I typed and passed with was one line: let done = BarButton.done(title: "Save")

Here's what I have for Swift 3.0:

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

let done = BarButton.done(title: "Save")
Phil Jones
Phil Jones
13,727 Points

This worked. Thanks, Joe :)

Zachary Betz
Zachary Betz
10,413 Points

Hi Matt, let's break the question down a bit.

To a constant named done assign an enum value of type Button with the member Done. This member takes an associated value; assign it the string "Done".

We know we need a constant named done

let done

And we need to assign an enum value of type Button with the member Done

let done = Button.Done

Lastly, this member takes an associated value, and we are to assign it the string "Done"

let done = Button.Done("Done")

Remember to write your code in the enums.swift file, beneath the Button enum