Enumeration in swift
Enumerations
An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.
enum Direction{
case north
case south
case east
case west
}
Note :
- Unlike C and Objective C, Here enum is not initialized with a default value.
- The name must start with a Capital letter.
var direction = Direction.west
or
var direction = .west
If the
case
for .west
is omitted, this code does not compile, because it does not consider the complete list of CompassPoint
cases.
When it is not appropriate to provide a
case
for every enumeration case, you can provide a default
case to cover any cases that are not addressed explicitly:switch somePlanet {
case .earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
- Associated Values
You can define Swift enumerations to store associated values of any given type, and the value types can be different for each case of the enumeration if needed.
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
ex.
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
switch productBarcode {
case .upc(let numberSystem, let manufacturer, let product, let check):
print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
case .qrCode(let productCode):
print("QR code: \(productCode).")
}
Raw Values
Enumeration cases can come prepopulated with default values (called raw values), which are all of the same types.
enum ASCIIControlCharacter: Character {
case tab = "\t"
case lineFeed = "\n"
case carriageReturn = "\r"
}
Raw values can be strings, characters, or any of the integer or floating-point number types. Each raw value must be unique within its enumeration declaration.
Raw value are not same as associated values. Raw values are set when you define the enumeration.
Implicitly Assigned Raw Values
you do not need to assign explicitly raw values for each and every case. It can be integer and string. It starts in increasing order. This means the next value will the next value of the previous raw value. if you do not provide any raw value then it will start with 0.
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
In the above venus will 2, the earth will 3, and so on.
Initializing from a Raw Value
If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value’s type (as a parameter called
rawValue
) and returns either an enumeration case or nil.
let possiblePlanet = Planet(rawValue: 7)
the raw value initializer always returns an optional enumeration case. In the example above,
possiblePlanet
is of type Planet?
, or “optional Planet
.”
NOTE
The raw value initializer is a failable initializer, because not every raw value will return an enumeration case. For more information, see Failable Initializers.
Recursive Enumerations
A recursive enumeration is an enumeration that has another instance of the enumeration as the associated value for one or more of the enumeration cases. Using the "indirect" keyword you can make recursive enumeration.
enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
You can also write
indirect
before the beginning of the enumeration, to enable indirection for all of the enumeration’s cases that need itindirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}
The code below shows the
ArithmeticExpression
recursive enumeration being created for (5 + 4) * 2
:let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
That's all from Swift Enumeration. Please, feel free to ask any queries and suggest a topic to write about. Please, don't forget to give me valuable feedback.
Happy Swift Coding 😊
Sure, Open for Suggestions.
ReplyDelete