Swift enumerations have both associated and raw values. But the use cases of these values is not clear to me. So I would really appreciate if anyone can explain the difference between the associated and raw values, an example would be very helpful.
The raw value for a particular enumeration case is always the same. Associated values are set when you create a new constant or variable based on one of the enumeration's cases, and can be different each time you do so.
We had to do this because Swift doesn't allow us to have both: raw values and associated values within the same enum. A Swift enum can either have raw values or associated values. Why is that? It's because of the definition of a raw value: A raw value is something that uniquely identifies a value of a particular type.
In Swift enum, we learned how to define a data type that has a fixed set of related values. However, sometimes we may want to attach additional information to enum values. These additional information attached to enum values are called associated values.
The association value of a stimulus is a measure of its meaningfulness. It is a strong predictor of how easy it is to learn new information about that stimulus, for example to learn to associate it with a second stimulus, or to recall or recognize it in a memory test.
Raw values are for when every case in the enumeration is represented by a compile-time-set value. The are akin to constants, i.e.
let A = 0
let B = 1
is similar to:
enum E: Int {
case A // if you don't specify, IntegerLiteralConvertible-based enums start at 0
case B
}
So, A
has a fixed raw value of 0
, B
of 1
etc set at compile time. They all have to be the same type (the type of the raw value is for the whole enum, not each individual case). They can only be literal-convertible strings, characters or numbers. And they all have to be distinct (no two enums can have the same raw value).
Associated values are more like variables, associated with one of the enumeration cases:
enum E {
case A(Int)
case B
case C(String)
}
Here, A
now has an associated Int
that can hold any integer value. B
on the other hand, has no associated value. And C
has an associated String
. Associated types can be of any type, not just strings or numbers.
Any given value of type E
will only ever hold one of the associated types, i.e. either an Int
if the enum is an A
, or a String
if the enum is a C
. It only needs enough space for the bigger of the two. Types like this are sometimes referred to as "discriminated unions" – a union being a variable that can hold multiple different types, but you know (from the enum case) which one it is holding.
They can even be generic. The most common example of which is Optional
, which is defined like this:
enum Optional<T> {
case .Some(T)
case .None
}
Swift Enum raw vs associated values
Enumerations
or Enum
allows you to create a finite set of values and enum variable references on a single value from the set
In Swift, an enum cannot have both raw values and associated values at the same time.
enum Color: String {
case white = "#ffffff"
case black = "#000000"
}
A "raw value" is an unique identifier of a type. It means that you are able to construct your type by ID. For example:
XCTAssertEqual(Color.white, Color(rawValue: "#ffffff"))
To get raw value use
Color.white.rawValue
enum Color {
case white
case black
case custom(hex: String)
}
Swift's "associated values" allows you to add additional information into enum that can be defined dynamically. Please note when we introduce "associated values", we omit the "raw values" and add a type annotation. This makes it impossible to use the "raw value" to reconstruct your type, because it is now set up dynamically.
You can read the "associated value" as follows:
let myColor = Color.custom(hex: "#ff00ff")
switch myColor {
case .custom(let hex):
print("custom color hex:\(hex)") //#ff00ff
case .white:
print("white color")
case .black:
print("black color")
}
Good example of Associated Value
enum is Result
[About]
Please note that Objective-C does not support Swift's enum(except Int-bound)
Also to campare
raw enum
if case .error = someResult {
//some logic
}
associated enum
if case .success(let result) = someResult, result == "ok" {
//some logic
}
[Swift indirect enum]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With