The Enum Basics

Welcome to the first lesson of Advanced Enum. Before you learn intermediate and advanced in later lessons, let us get everyone on the same page.

Problem

Let’s Review

Types of Enumeration

  1. Basic Enumerations
  2. Enumerations that have Raw Values
  3. Enumerations that have Associated Values

Basic Enumerations

Create an enum called Compass. It contains 4 cases.

  1. enum Compass {
  2. case north
  3. case south
  4. case east
  5. case west
  6. }

You define multiple cases in a single line

  1. enum Planet {
  2. case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
  3. }

To initialize, you choose one of the cases.

  1. let earth = Planet.earth // init

You may run through a switch statement to identify the enum type.

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

Raw Value

An enumeration may contain value. Swift supports the following types for the value of an enum:

  • Int
  • Float
  • String
  • Bool

Raw Value: String

  1. enum Food: String {
  2. case pizza
  3. case banana
  4. case chicken
  5. case bigMac
  6. }
  7. let stringValueFromPizza = Food.pizza.rawValue
  8. print(stringValueFromPizza) // pizza

Raw Value: Int

  1. enum Days: Int {
  2. case mon, tue, wed, thu, fri = 10, sat, sun
  3. }
  4. // mon = 0, tue = 1, wed = 2, ... , fri = 10, sat = 11
  5. let myDay = Days.fri.rawValue
  6. print(myDay) // 10

Initialization from Raw Value

You may create an enum object using a raw value. it may fail. Therefore, the returned object may be nil.

  1. let possibleeDay = Days(rawValue: 10) // returns optional
  2. print(possibleeDay!)

You may combine with a switch statement.

  1. if let someDay = Days(rawValue: 3) {
  2. switch someDay {
  3. case .sat, .sun:
  4. print("Weekends!!")
  5. default:
  6. print("Weekdays!")
  7. }
  8. } else {
  9. print("No such day exists")
  10. }

Associated Value

Each case may contain value along with it.

Example from Doc

  1. enum Barcode {
  2. case upc(Int, Int, Int, Int)
  3. case qrCode(String)
  4. }
  5. var qrCode = Barcode.qrCode("XYZ")
  6. var upcCode = Barcode.upc(4, 2, 5, 5)

Validation

Determine if the instance is Barcode.qrCode using an else-if statement. The process is similar to implicit unwrapping.

  1. if case let Barcode.qrCode(value) = qrCode {
  2. print("This is a qrcode")
  3. print(value)
  4. }

You may capture the associated value of the instance, qrCode using case let. You’ve named the captured value as value.

Determine whether he upcCode instance is Barcode.upcCode.

  1. if case let Barcode.upc(numberSystem, manufacturer, product, check) = upcCode {
  2. print("numbersystem:", numberSystem)
  3. print("manufaturer:",manufacturer)
  4. print("product:",product)
  5. print("check:",check)
  6. }

Validation with Switch Statement

Instead of checking each enum object individually using an else-if, you may use a switch statement.

  1. let code = upcCode
  2. switch code {
  3. case .upc(let numberSystem, let manufacturer, let product, let check):
  4. print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
  5. case .qrCode(let productCode):
  6. print("QR code: \(productCode).")
  7. }

The code below is identical as above. It gives off zenness.

  1. switch code {
  2. case let .upc(numberSystem, manufacturer, product, check):
  3. print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")
  4. case let .qrCode(productCode):
  5. print("QR code: \(productCode).")
  6. }

Source Code

7001_enum_basics.playground

Conclusion

You’ve reviewed the three types of enums in the Swift Programming Language. If you are not comfortable with any, make sure you review and watch this video multiple times to get used to the syntax. Upcoming lessons will get more complex.

In the following lesson, you will learn how to use Swift enums to type less but produce more using practical examples in iOS development.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.