A summary of use enum in Swift

· 2 min read
A summary of use enum in Swift

Enumeration type is a common type in many programming languages, which normally presents a set of limited status for a program, such as list all the directions for a compass:

enum CompassPoint {
    case north
    case south
    case east
    case west
}

However, enum provides more features than that:

1. Static values

enum is often to be used as type constants.

For example, we have three product types, now we have a ProudctType which contains all the types, so we can use that ProudctType as constant value anywhere in the project. The benefits of doing such is all the product type in one place and they can be accessed through .:

enum ProudctType {
	static let limtedOffer = "Limted Offer"
	static let onGoingOffer = "On-going Offer"
	static let tempOffer = "Temporary only Offer"
}


print(PersonTypes.limtedOffer) // Limted Offer

2. Enumerations with raw values

Unlike objective-c and many other programming languages, enumeration cases in Swift don't have an integer value by default, but we can always assign a custom value to it.

enum ProductType: String {
    case limtedOffer = "Limted Offer"
    case onGoingOffer = "On-going Offer"
    case tempOffer = "Temporary only Offer"
}

// We can also retrieve the enum type via the raw value, it will return nil if no matches
let productType = productType(rawValue: "Limted Offer") 

3. Enumerations with associated values

Associated values allow us to pass values from definition.

// Example is from the Swift doc
enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}

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).")
}

4. Can't use stored properties

enum MyError: Error {
    var error1: String = "error 1"
}
// ERROR: throw out enums must not contain stored properties.
// To fix this error, we can use a computed property here:

enum MyError: Error {
  var error1: String {
    get {
        return "error 1"
    }
  }
}

5. All cases needed to be iterated with switch


It is recommended by the compiler in many other programming languages that we need to list all the cases while doing switch. However it is mandatory in swift:

enum MyError: Error {
    case error1
    case error2
    case error3
}


// Example 1: 
let err = MyError.error1
switch err {
case .error1:
    print("Error 1")
case .error2:
    print("Error 2")
}
// ERROR: Switch must be exhaustive. 
// In order to fix this we need either:
// Having all the enum types, see example 2, or 
// Using a break

// Example 2:
switch err {
case .error1:
    print("Error 1")
case .error2:
    print("Error 2")
case .error3:
    print("Error 3")
}

// Example 3:
switch err {
case .error1:
    print("Error 1")
case .error2:
    print("Error 2")
default:
    break
}