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 trialAyo Omotosho
4,855 PointsI'm Stuck! Some help with this would be highly appreciated.
In the editor I've created an enum to manage text presentation in my app. The enum members represent the three different text options. Your task is to create a computed property, style, that returns the correct style specifier provided below. For example Text.Headline.style should return the string "UIFontTextStyleHeadline".
Can't seem to figure out, how exactly i am supposed to write this code....please help
let UIFontTextStyleHeadline = "UIFontTextStyleHeadline"
let UIFontTextStyleBody = "UIFontTextStyleBody"
let UIFontTextStyleFootnote = "UIFontTextStyleFootnote"
enum Text {
case headline
case body
case footnote
}
struct style {
var text = Text()
}
3 Answers
Jeff McDivitt
23,970 PointsI would not use a struct to do this although you probably could. You are just creating a computed property inside of the enum declaration. Since the property is a readonly you will not need a getter or setter. You are switching on the instance that the property has access to (self). Based on the enum case you return the respective style.
Jeff McDivitt
23,970 PointsThis is one way you could do it, please let me know if you need more explanation
let UIFontTextStyleHeadline = "UIFontTextStyleHeadline"
let UIFontTextStyleBody = "UIFontTextStyleBody"
let UIFontTextStyleFootnote = "UIFontTextStyleFootnote"
enum Text {
case headline
case body
case footnote
var style: String {
switch self {
case .headline:
return UIFontTextStyleHeadline
case .body:
return UIFontTextStyleBody
case .footnote:
return UIFontTextStyleFootnote
}
}
}
Ayo Omotosho
4,855 PointsThanks alot Jeff! This got me through the challenge! But if you don't mind, i'd like you to please shed some light on this process. Also, can one use a struct to do this?
Dan Poynor
Data Analysis Techdegree Graduate 62,952 PointsI think there's a typo in the objective definition, specifically this text:
For example Text.Headline.style should return the string "UIFontTextStyleHeadline".
should use lowercase .headline:
For example Text.headline.style should return the string "UIFontTextStyleHeadline".
Also, since the objective is to call the computed property .style on Text (a type), style should probably be defined with the static keyword, as in:
static var style: String {...}
The code checker doesn't test this though.
Ayo Omotosho
4,855 PointsAyo Omotosho
4,855 PointsGot it! Thanks. Cheers!