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 trialMaxence Roy
8,753 PointsNeed help with enum method
Having a hard time with enums! Can you guide me for this challenge?
// 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(title: String) -> UIBarButtonItem {
switch self {
case .done: return UIBarButtonStyle.done
case .edit: return UIBarButtonStyle.plain
}
}
}
let done = BarButton.done(title: "Save")
4 Answers
Kayondo Martin
7,481 PointsI think the problem is with the return statements in the switch
something like
switch self {
case .done(let title): return UIBarButtonItem(title: title, style: UIBraButtonItemStyle.done, target: nil, action: nil)
case .edit(let title): return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
}
the call it on the done constant initiated
let done = BarButton.done(title: "Save")
let button = done.button()
Maxence Roy
8,753 PointsSo this is the code that I've wrote based on your advice and this is what I came up with :
enum BarButton {
case done(title: String)
case edit(title: String)
func button(title: String) -> UIBarButtonItem {
switch self {
case .done(let title): return UIBarButtonItem(title: title, style: UIBarButtonItemStyle.done, target: nil, action: nil)
case .edit(let title): return UIBarButtonItem(title: title, style: UIBarButtonItemStyle.plain, target: nil, action: nil)
}
}
}
let done = BarButton.done(title: "Save")
let button = done.button(title: "Save")
It's not compiling, do you know why ?
Kayondo Martin
7,481 PointsIn case .edit, the style is .plain, not UIButtonItemStyle.plain
Maxence Roy
8,753 PointsHi Kayondo,
I made the change and it still does not compile :
enum BarButton {
case done(title: String)
case edit(title: String)
func button(title: String) -> UIBarButtonItem {
switch self {
case .done(let title): return UIBarButtonItem(title: title, style: UIBarButtonItemStyle.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(title: "Save")
Kayondo Martin
7,481 PointsI'm sorry, Max, I forgot to point out that the function takes no arguments...
fun button()->UIBarButton {
switch self {
case .done(let title): return UIBarButtonItem(title: title, style: UIBarButtonItemStyle.done, target: nil, action: nil)
case .edit(let title): return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
}
}
Same as for declaring the constant button, please omit the argument like I did at first.
Maxence Roy
8,753 PointsThank you! It works.