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

Will Van de Walle
Will Van de Walle
2,352 Points

Code Challenge: What?

I am utterly confused about the code challenge. I've taken a long break from this course and I'm trying to get back into it, and I just don't understand how to do this with enums. There doesn't seem to be any connection between the constants and the enum cases, and I'm just confused. Help?

2 Answers

Hello, Will.

If I remember correctly, the challenge asks you to create a computed property that returns a string.

Try creating the computed property within the enum, and switching on it(self). For each case, it should return the appropriate string. The string have been assigned to the constants. Also, consider watching the lesson again to brush up on computed properties.

If I've been vague, do ask away for clarification.

I am sorry but I am also having the same problem could you clarify or share what you did? Please. Much appreciated

Hi, Christian.

  • To start you off, you need a computed property named style that returns a string :
var style: String {}
  • Secondly, you need to return a string for each enum case. Giving that this is an enum, I think a switch statement would fit perfectly.

  • The computed property is within the enum, and the switch will be within the enum too. Thus, you need to use the keyword self to switch on.

  • I believe there are 3 constants, above the enum, containing the appropriate string for each enum case. Use those instead of passing in the string directly.

  • Here's a template:

let first = "string"
let second = "string"
let third = "string"

enum numbers {
    case 1
    case 2
    case 3

    var style: String {
        switch self {
              case .1: return first
              case .2: return second
              case .3: return third
        }
     }
}