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 trial

iOS Intermediate Swift Properties Computed Properties

Enrico Trombini Charavara
Enrico Trombini Charavara
7,220 Points

Computed Properties -> Swift

I looked for the answer to this challenge in several discussions in the community but I did not find the answer. I do not understand how the property can be implemented in this case, I wanted an example if possible.

2 Answers

Aniruddha Shukla
Aniruddha Shukla
2,846 Points

Every time the caller calls style the string will be calculated on the fly. You can add it in the following manner

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
        }
    }
}