Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Intermediate Swift Properties Computed Properties

Richard Pitts
PLUS
Richard Pitts
Courses Plus Student 1,243 Points

Not sure here...

What is the correct answer?

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

enum Text {
  case headline
      var style = UIFontTextStyleHeadline
  case body
  case footnote
  case
}

2 Answers

Dan Lindsay
Dan Lindsay
39,611 Points

Hi Richard,

So the task at hand is to create a computed property called style that returns the correct style specifier below, which we can see have String values.

So in our Text enum, we need to add that style property after the three cases, not in the middle of them, as you started to. Knowing we have a few cases to go through, it looks like this is a job for a switch statement. So we start with this:

var style: String {

}

Now because we are inside the Text enum, we will need to switch on self. So we add this to our property:

var style: String {
    switch self {

    }
}

We can now add our cases, referencing them with dot notation, and set the return statements to the appropriate constants, which are String values, and why we declared our var to be of type String. Here is the final code that will pass the challenge:

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

I hope this isn’t too complex an explanation, I know I struggled with this one too. Let me know if this helped.

All the best,

Dan