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

Why do Swift’s enums have raw values?

Paul Hudson    @twostraws   

Updated for Xcode 15

Think about an enum such as this one:

enum Mood {
    case happy
    case sad
    case grumpy
    case sleepy
    case hungry
}

That lets us use values such as Mood.happy in our code, which is much safer and more efficient than storing “happy” as a string.

Now think about stuff outside our code – if we were reading the user’s saved data, or downloading something from the internet. Sure, our Swift code knows what Mood.happy means, but how could we send that value over the internet?

I know this sounds a bit philosophical, but I want you to think about what Mood.happy really is. How is it stored when our program runs? The point is that we don’t really care most of the time – Swift could internally store it as the number 556, and it wouldn’t make any difference. All we care about is that we get the safety and performance that enums bring.

However, things get more complex when we do need to know how the value is stored. If we need to download a list of users from the internet and know what their current mood is, then that server needs to be able to send that data in a way we can understand.

That’s where enum raw values come in: they let us use enums just like we normally would, but also attach an underlying value to each case. Inside our Swift code this mostly won’t have any effect, but it does mean we now have a specific, fixed way of referring to each value for the times we need it.

So, for our Mood enum we could ask Swift to provide integer values for each of our cases like this:

enum Mood: Int {
    case happy
    case sad
    case grumpy
    case sleepy
    case hungry
}

In our code we can carry on writing Mood.happy, Mood.sad, and so on, just like before. However, now we can also download some data from a server, and be told “this user has mood 0,” and match that up with Mood.happy.

If you’re keen to keep learning more about enums, I can recommend Antoine van der Lee’s article on the topic: https://www.avanderlee.com/swift/enumerations/

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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!

Average rating: 4.7/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.