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 trialMichael Jones
Python Development Techdegree Graduate 38,554 PointsCan someone help me with the Computed Properties challenge.
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".
let UIFontTextStyleHeadline = "UIFontTextStyleHeadline"
let UIFontTextStyleBody = "UIFontTextStyleBody"
let UIFontTextStyleFootnote = "UIFontTextStyleFootnote"
enum Text {
case Headline
case Body
case Footnote
}
1 Answer
Jhoan Arango
14,575 PointsHello Michael :
You can treat computer properties like functions. You can add switch statements into them as well.
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
}
}
}
Hope this helps
Michael Jones
Python Development Techdegree Graduate 38,554 PointsMichael Jones
Python Development Techdegree Graduate 38,554 PointsThank you. This does help.
Michael Jones
Python Development Techdegree Graduate 38,554 PointsMichael Jones
Python Development Techdegree Graduate 38,554 PointsA little confused to how this worked without any errors. Don't we need to have to use default when using a Switch statement??
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsNot when the switch is exhaustive .. In this case we went through all the possible values in the enum.
Joshua Hardy
17,317 PointsJoshua Hardy
17,317 Points.Headline .Body & .Footnote need to be lowercase. The program must have been updated.
Eddie Aguilar
2,393 PointsEddie Aguilar
2,393 PointsIs the above answer the only possible way to complete this challenge?? I did not even think about using a switch statement.