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 trialVansh Agarwal
Courses Plus Student 3,965 PointsCan someone help me with this code challenge?
I don't know what's the problem. Please help me out, thanks!
// Example of UIBarButtonItem instance
// let someButton = UIBarButtonItem(title: "A Title", style: .plain, target: nil, action: nil)
enum UIBarButtonStyle{
case done
case style
}
class UIBarButtonItem{
let title: String
let style: UIBarButtonStyle
let target: Int
let action: Int
init (title: String, style: UIBarButtonStyle, target: Int, action: Int){
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 UIBarButton(title: title, style: UIBarButtonStyle.done, target: nil, action: nil)
case .edit(let title): return UIBarButton(title: title, style: UIBarButtonStyle.plain, target: nil, action: nil)
}
}
}
let done = BarButton.done(title: "Save")
let button = done.button()
2 Answers
Leticia Rodríguez
iOS Development with Swift Techdegree Student 3,455 PointsHi!
It's not necessary to create UIBarButtonItem class nor the UIBarButtonStyle's enum.
Class UIBarButtonItem is already provided by swift when you import UIKit class.
All you need to do is call the init method in each case of switch statement, like it follows:
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")
And then invoke the method button:
let button = done.button()
The letter of this exercise was a little confusing to me too! Hope it helps!
jc laurent
6,351 Pointsnobody is asking you to create a class, UIbarbutton enum + a function and tons of code etc...
work with the code provide and the instructions pls
Vansh Agarwal
Courses Plus Student 3,965 PointsVansh Agarwal
Courses Plus Student 3,965 PointsThanks, this really helped!