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 Proxyman: A high-performance, native macOS app for developers to easily capture, inspect, and manipulate HTTP/HTTPS traffic. The ultimate tool for debugging network traffic, supporting both iOS and Android simulators and physical devices.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.