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 2 Properties Computed Properties

Can 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".

enum.swift
let UIFontTextStyleHeadline = "UIFontTextStyleHeadline"
let UIFontTextStyleBody = "UIFontTextStyleBody"
let UIFontTextStyleFootnote = "UIFontTextStyleFootnote"

enum Text {
  case Headline
  case Body
  case Footnote
}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello 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

Thank you. This does help.

A 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
Jhoan Arango
14,575 Points

Not when the switch is exhaustive .. In this case we went through all the possible values in the enum.

.Headline .Body & .Footnote need to be lowercase. The program must have been updated.

Is the above answer the only possible way to complete this challenge?? I did not even think about using a switch statement.