Notice: Undefined index: understanding-swift in /var/sites/ on line 89
Why would you want to associate a value with an enum case? - a free tutorial

NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Why would you want to associate a value with an enum case?

Paul Hudson    @twostraws   

One of the most powerful features of Swift’s enums is their ability to store one or more associated values – extra pieces of information that describe the enum case in more detail.

Associated values can be attached to every case, or only some cases. Plus, each case can have as many associated values as you want, as long as each one has a type.

For example, we might create a Weather enum with three cases:

enum Weather {
    case sunny
    case windy(speed: Int)
    case rainy(chance: Int, amount: Int)

That means our weather can be sunny, it can be windy, or it can be rainy. However, when it’s windy we’re also asking to store how fast the wind is as an integer – whether it’s 10 kilometers per hour (kph), 20, 30, and so on. And when it’s rainy, we’re storing a percentage chance of the rain happening and a volume value, storing how much rain will store.

We could do something similar, but it would be pretty unpleasant. Imagine this:

enum Weather {
    case sunny
    case lightBreeze
    case aBitWindy
    case quiteBlusteryNow
    case nowThatsAStrongWind
    case thisIsSeriousNow
    case itsAHurricane

That gives us more ways to describe windy weather, but it’s very imprecise – we’ve gone from being able to distinguish between a 10kph wind and a 15kph wind. Worse, if you hadn’t seen the list ordered like it is above, would you know that aBitWindy was supposed to be stronger than lightBreeze but less strong than quiteBlusteryNow?

Now imagine trying to work with the rainy case, where we need to store two integers. We’d end up having to have cases that describe a low chance of light rain, a low chance of moderate rain, a low chance of heavy rain, a moderate chance of light rain, a moderate chance of moderate rain… well, you get the point.

So, enums with associated values let us add extra information to an enum case – think of them as being adjectives to a noun, because it lets us describe the thing in more detail.

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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

Buy Pro Swift 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 (Vapor Edition) 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 Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Link copied to your pasteboard.