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 Enumerations and Optionals in Swift Introduction to Enumerations Methods on Enumerations

About functions

I did this and it works

enum Text {
  case headline
  case body
  case footnote

    func style () -> String {
        switch self {
        case .headline: return "UIFontTextStyleHeadline"
        case .body: return "UIFontTextStyleBody"
        case .footnote: return "UIFontTextStyleFootnote"
        }
    }
}

print(Text.footnote.style())

it prints:

UIFontTextStyleFootnote

However when I do the next it doesn't work.

enum Text: String {
  case headline = "UIFontTextStyleHeadline"
  case body = "UIFontTextStyleBody"
  case footnote = "UIFontTextStyleFootnote"

    func style (_ value: Text ) -> String {
        switch value {
        case .headline: return Text.headline.rawValue
        case .body: return Text.body.rawValue
        case .footnote: return Text.footnote.rawValue
        }
    }
}

print(Text.style(.body))

It prints:

(function)

Please I am really curious about this, why this happens?

1 Answer

Hello,

Sorry it took a few days to get to you, I was in vacations.

So essentially what you are doing should work in theory, but there Is one thing missing.

Noticed how in the first snippet you called : print(Text.footnote.style()) this is correct since you are creating an instance of Text and then calling its method.

ON the second snippet you are NOT creating an intense of Text, therefore you are not able to call the "method" instead you are creating a closure. When I say creating a closure is because when you call Text.style() it will return that function and you can set it on a variable if you wanted. This variable will be that closure. Hence the reason why is printing "(Function)".

What you need to do is what you did on the first snippet:

enum Text: String {

  case headline = "UIFontTextStyleHeadline"
  case body     = "UIFontTextStyleBody"
  case footnote = "UIFontTextStyleFootnote"

    func style (_ value: Text) -> String {
        switch value {
        case .headline : return Text.headline.rawValue
        case .body     : return Text.body.rawValue
        case .footnote : return Text.footnote.rawValue
        }
    }
}

print(Text.headline.style(.body)) // prints "UIFontTextStyleBody"

Hope this helps you, let me know if you need more help