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

Brian Uribe
Brian Uribe
3,488 Points

Works in playground but not here

I used this code in playground and I was able to call the text, but it won't work here. Why?

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

enum Text {
    case Headline
    var headlineStyle: String{
        return "UIFontTextStyleHeadline"
    }
    case Body
    var bodyStyle: String{
        return "UIFontTextStyleBody"
    }
    case Footnote
    var footNoteStyle: String{
        return "UIFontTextStyleFootnote"    }


}
Andrey Kondratyuk
Andrey Kondratyuk
4,583 Points

I had a lot of trouble with this one too. Because it's an enum, they want you to use a switch statement for the different cases. You also need a computed property named style specifically.

I don't know if I should post the answer here or not but let me know if you're still stuck.

Edit: Hey Team Treehouse, we were confused because this question is in the the wrong place. The video that explains how to do this is right after the question.

1 Answer

Brian, nice try, but you have 3 vars, none of which is style. This was tricky, as the previous videos never mention computed properties in enums.

Try this:

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