Junior — Middle
What is the difference between raw and linked values in enum definition in programming language?
sobes.tech AI
Answer from AI
In Swift language, an enum can have two types of values: raw values and associated values.
Raw Values are fixed values of a single type (e.g., String, Int) assigned to each enum case. They are defined at declaration and do not change. They allow easy conversion between the enum and the underlying type.
enum Direction: String {
case north = "N"
case south = "S"
case east = "E"
case west = "W"
}
Associated Values are additional data that can be attached to a specific enum case when it is created. They can be of different types and allow storing extra information for each case.
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
let productBarcode = Barcode.upc(8, 85909, 51226, 3)
Thus, raw values are fixed constants associated with cases, while associated values are dynamic data that can be passed along with the case during usage.