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 trialChase Maddox
Courses Plus Student 5,461 PointsConfused 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
// 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
iOS Development Techdegree Graduate 15,223 PointsHey 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()
Gary Nguyen
921 PointsCould the last two lines be condensed to
let done = BarButton.done(title: "Save").button()
?
Chase Maddox
Courses Plus Student 5,461 PointsChase Maddox
Courses Plus Student 5,461 PointsAhh ok. Thanks Garrett, I really appreciate the help.