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

computed property help

really not sure how to do this at all. i understand what is being done in the video, but cant seem to put it to use here.

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

enum Text {
  case Headline
  case Body
  case Footnote

}

var style: String {

    get {

        let h = Text.Headline
        let b = Text.Body
        let f = Text.Footnote

        return String

    }

    set (value) {

        Text.Headline = value.Headline
        Text.Body = value.Body
        Text.Footnote = value.Footnote
    }

}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Todd Stacy,

You're overcomplicating things. What we need to do here is create a computed property called style, inside of the enum declaration. This property will be a readonly computed property. For the getter method, we will switch on the instance this property is accessed on, also known as self. Based on the matching enum case, we will return its respective style.

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

Good Luck

i over think way too much. thank you for all your help!