UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

What makes Swift's enums so powerful and what are enums with associated values?

Recorded – watch the full episode on YouTube.

Could you tell us what you think makes Swift enums just so powerful? How would you explain enums with associated values to someone who's never come across that concept before?

Paola Mata: To me, it's really the flexibility. You can use them in a really simple work case. To define some simple values, integers, whatever. But you can also use them anywhere for networking reasons, for analytics. There's just like a myriad of ways they can be used. And whether in a simple form or the more complex, using associated types. Also what I really like about enums is the safety feature of being able to switch when you have the predefined values and making sure you're implementing or accounting for each case.

"You can use them in a really simple work case. To define some simple values, integers, whatever. But you can also use them anywhere for networking reasons, for analytics. There's just like a myriad of ways they can be used."

Paul Hudson: So when it comes to folks making a choice, they want to choose to be enums and a struct. When would you say, "Yeah, this is definitely a case for an enum," and when, "Not so much?"

Paola Mata: Specifically when you have a list of predefined values that you want to group under a single type. An enum is something... Sorry. A struct would have more property, it's a little bit more open. So I'm thinking more real world examples. We have the concept of an item in my code base that's a collectible. So you can collect recipes or you can collect collections. So we might have an enum for anything that can be collectible. And some of them have associated value. Some of them don't. Also something like air handling or something like that, different potential errors that you can receive.

Paul Hudson: Certainly one pattern I've seen quite a lot is when I do the same thing in my own code. When I think about some values or some functions that I want to put somewhere grouped, like some static stuff. And they could totally go on a struct, but increasingly focus putting them in enums instead. With the underlying knowledge that you cannot instantiate the enum. You could make one of these structs but you shouldn't make one of these structs. They're not designed to be made, they're designed to be consumed and used somehow. And enums can get to exactly the same purpose. You can give them constance, you can give them methods, you can give them whenever you want to. Make them generic if you really want to. But you can't then create an instance of that enum. It isn't possible.

Paola Mata: That's again, one of the really great safety features that we have. I think... I forgot what I was going to say.

"You can actually use a struct type that you've defined as an associated value."

Paul Hudson: I'm sure it was great. There's no pressure. These people can wait. It's fine. They can all wait.

Paola Mata: We have all day.

Paul Hudson: Well, there's the question here, and this is a complex one, because it's going back to the idea of, can you explain something complicated in a fairly easy way. And how would you explain enums with associated values to someone who's never come across that concept before?

Paola Mata: Honestly, I'd probably try to use the example that I just used with the types of collections that they can have. There are some cases where, just having the raw value isn't enough. You might want some more information. For example, if one of the cases is a recipe, I might want the recipe itself. I might want to be able to pull out from the enum case this value that's associated with that type. It allows us to attach more information to a specific case within that type.

I thought of what I was going to say before is that you can actually use a struct type that you've defined as an associated value.

This transcript was recorded as part of Swiftly Speaking. You can watch the full original episode on YouTube, or subscribe to the audio version on Apple Podcasts.

Listen on Apple Podcasts

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.