As well as storing a simple value, enums can also store associated values attached to each case. This lets you attach additional information to your enums so they can represent more nuanced data.
For example, we might define an enum that stores various kinds of activities:
enum Activity {
case bored
case running
case talking
case singing
}
That lets us say that someone is talking, but we don’t know what they are talking about, or we can know that someone is running, but we don’t know where they are running to.
Enum associated values let us add those additional details:
enum Activity {
case bored
case running(destination: String)
case talking(topic: String)
case singing(volume: Int)
}
Now we can be more precise – we can say that someone is talking about football:
let talking = Activity.talking(topic: "football")
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.