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.
  1. enum Direction{
  2. case north
  3. case south
  4. case east
  5. case west
  6. }

Note :
  • Unlike C and Objective C, Here enum is not initialized with a default value.
  •  The name must start with a Capital letter.
  1. var direction = Direction.west
  2. or
  3. 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:

  1. switch somePlanet {
  2. case .earth:
  3. print("Mostly harmless")
  4. default:
  5. print("Not a safe place for humans")
  6. }

  7. 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.

  1. enum Barcode {
  2. case upc(Int, Int, Int, Int)
  3. case qrCode(String)
  4. }
  5. ex.
  6. var productBarcode = Barcode.upc(8, 85909, 51226, 3)
  1. switch productBarcode {
  2. case .upc(let numberSystem, let manufacturer, let product, let check):
  3. print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
  4. case .qrCode(let productCode):
  5. print("QR code: \(productCode).")
  6. }

Raw Values

   Enumeration cases can come prepopulated with default values (called raw values), which are all of the same types.

  1. enum ASCIIControlCharacter: Character {
  2. case tab = "\t"
  3. case lineFeed = "\n"
  4. case carriageReturn = "\r"
  5. }
   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.

  1. enum Planet: Int {
  2. case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
  3. }
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.

  
  1. 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

    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.

  1. enum ArithmeticExpression {
  2. case number(Int)
  3. indirect case addition(ArithmeticExpression, ArithmeticExpression)
  4. indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
  5. }

You can also write indirect before the beginning of the enumeration, to enable indirection for all of the enumeration’s cases that need it

  1. indirect enum ArithmeticExpression {
  2. case number(Int)
  3. case addition(ArithmeticExpression, ArithmeticExpression)
  4. case multiplication(ArithmeticExpression, ArithmeticExpression)
  5. }

The code below shows the ArithmeticExpression recursive enumeration being created for (5 + 4) * 2:

  1. let five = ArithmeticExpression.number(5)
  2. let four = ArithmeticExpression.number(4)
  3. let sum = ArithmeticExpression.addition(five, four)
  4. 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 😊

Comments

Post a Comment

Popular posts from this blog

Copy Vs Mutable Copy in iOS

How to Disable iOS/iPad App availability on Mac Store, Silicon?

In Swift: NSBlockOperation and NSOperationQueue Sample