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

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