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 trialLokesh Gnanasekhar
1,840 PointsShowing compiler errors and nothing is previewed in preview window
Every appears correct in the playground , but still showing errors. Tried adding UIKit too but still same issue
// 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 doneButton = UIBarButtonItem(title: "A Title", style: .done, target: nil, action: nil)
return doneButton
case .edit(let title): let editButton = UIBarButtonItem(title: "A Title", style: .plain, target: nil, action: nil)
return editButton
}
}
}
let button = BarButton.done(title: "Save").button()
4 Answers
Nico Schmidt
12,781 PointsYou are quite right. The script is quite strict. So it should look like that.
let done = BarButton.done(title: "Save")
let button = done.button()
Nico Schmidt
12,781 PointsHint: You want to bind the associated value of title to the correct enum member. Currently you use βA Titleβ.
Lokesh Gnanasekhar
1,840 PointsI Corrected to title instead of "A Title" But still it says compiler error. But in xcode playground its not showing any error
Nico Schmidt
12,781 PointsYou can now return directly the UIButtonItem without declaring a constant.
Lokesh Gnanasekhar
1,840 PointsOn returning it directly its throwing error like this - Bummer! Make sure you're assigning the enum value to a constant named done. But as per task i should assign it to a "button" constant .
Task : Next, add a method to the enum named button that returns an instance of the class UIBarButtonItem configured properly. An example of how to create an instance for this task is shown in the comments below. Use the same initializer.
In the method, using the associated values as titles for the button, return a button with style UIBarButtonStyle.done for the done member of the BarButton enum. Similarly for the edit member, return a UIBarButtonItem instance with the style set to UIBarButtonStyle.plain.
In both cases you can pass nil for target and action. Once you have a method, call it on the value we created in the previous task and assign it to a constant named button.
Solution:
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 button = BarButton.done(title: "Save").button()
Nico Schmidt
12,781 PointsMy pleasure.
Lokesh Gnanasekhar
1,840 PointsLokesh Gnanasekhar
1,840 PointsThank you so much. Issue resolved. The editor is too strict .