April 2026
Intermediate
683 pages
25h 29m
English
Just like you can plant functions into the main flow, you can plant functions right into an enumeration too. Like computed properties, enumeration functions can access the current case via the self keyword.
Listing 7.22 features such an example through an enumeration called Mood. This enumeration contains the reflectToText function, which transforms the given text based on the mood at hand.
// Enumenum Mood { case happy case neutral case angry func reflectToText(_ text: String) -> String { switch self { case .happy: return text + ", hooray!" case .neutral: return text case .angry: return text.uppercased() + "!!!" } }}// Main flowlet myMood = Mood.happyprint(myMood.reflectToText("I'm learning Swift")) // I'm ...
Read now
Unlock full access