Enumerations
enum group of related values. same like c. not required to provide value to each of enum. Value can be string , character, float , int type. If u want u can specify the literal values of any kind. (for class object values need to use protocols Equatable, RawRepresentable )
Ex:
enum CompassPoint {
case North
case South
case East
case West
}
Each enumeration definition defines a brand new type.
Enum type name should start with capital letter.
Associated Values ( Mainly used to keep the two or more different kind of data types in one)
Ex Inventory system need to track product with two different types of barcode 1.with 1D barcodes 2. with 2D barcodes
enum Barcode {
case UPCA(Int, Int, Int, Int)
case QRCode(String)
}
var productBarcode = Barcode.UPCA(8, 85909, 51226, 3)
productBarcode = .QRCode("ABCDEFGHIJKLMNOP")
same variable can contain any kind of barcode . but container of variable and constant store one type of value at time i.e either UPCA or QRCode in above case. Not both at a time.
Usage in switch case
switch productBarcode {
case .UPCA(let numberSystem, let manufacturer, let product, let check):
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case .QRCode(let productCode):
print("QR code: \(productCode).")
}
Raw Values
Called as default values. i.e. value assigned to cases. Each raw value must be unique within its enumeration declaration.
ex:
enum ASCIIControlCharacter: Character {
case Tab = "\t"
case LineFeed = "\n"
case CarriageReturn = "\r"
}
When strings are used for raw values, the implicit value ( value assigned automatically like c if 0 assigned to first then 1, 2, next onwards) for each case is the text of that case’s name.
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. You can use this
initializer to try to create a new instance of the enumeration.
This example identifies Uranus from its raw value of 7:
let possiblePlanet = Planet(rawValue: 7)
Recursive Enumerations
In this case an enumeration has another instance of the enumeration as associated value.
To indicate enumeration is recursive use indirect keyword before it which tell compiler to insert necessary layer of indirection.
Ex:
enum ArithmeticExpression {
case Number(Int)
indirect case Addition(ArithmeticExpression, ArithmeticExpression)
indirect case Multiplication(ArithmeticExpression, ArithmeticExpression)
}
to enable all the enumeration cases as indirect write indirect before enumeration.
indirect enum ArithmeticExpression {
case Number(Int)
case Addition(ArithmeticExpression, ArithmeticExpression)
case Multiplication(ArithmeticExpression, ArithmeticExpression)
}
This enumeration can store a plain number, the addition of two expressions, and the multiplication of two expressions.
func evaluate(expression: ArithmeticExpression) -> Int {
switch expression {
case let .Number(value):
return value
case let .Addition(left, right):
return evaluate(left) + evaluate(right)
case let .Multiplication(left, right):
return evaluate(left) * evaluate(right)
}
}
0 comments:
Post a Comment