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

Ayo Omotosho
Ayo Omotosho
4,855 Points

I'm Stuck! Some help with this would be highly appreciated.

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

Can't seem to figure out, how exactly i am supposed to write this code....please help

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

enum Text {
  case headline
  case body
  case footnote
}

struct style {
  var text = Text()

}

3 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

I would not use a struct to do this although you probably could. You are just creating a computed property inside of the enum declaration. Since the property is a readonly you will not need a getter or setter. You are switching on the instance that the property has access to (self). Based on the enum case you return the respective style.

Ayo Omotosho
Ayo Omotosho
4,855 Points

Got it! Thanks. Cheers!

Jeff McDivitt
Jeff McDivitt
23,970 Points

This is one way you could do it, please let me know if you need more explanation

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
        }
    }
}
Ayo Omotosho
Ayo Omotosho
4,855 Points

Thanks alot Jeff! This got me through the challenge! But if you don't mind, i'd like you to please shed some light on this process. Also, can one use a struct to do this?

Dan Poynor
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dan Poynor
Full Stack JavaScript Techdegree Graduate 49,883 Points

I think there's a typo in the objective definition, specifically this text:

For example Text.Headline.style should return the string "UIFontTextStyleHeadline".

should use lowercase .headline:

For example Text.headline.style should return the string "UIFontTextStyleHeadline".

Also, since the objective is to call the computed property .style on Text (a type), style should probably be defined with the static keyword, as in:

static var style: String {...}

The code checker doesn't test this though.