Summary of some minor USES of the Swift enumeration

  • 2020-06-12 10:46:54
  • OfStack

preface

In Swift, enumerations are a very convenient and powerful type. We often use it in our daily use.

For example, our most common optional:


enum Optional<T> {
 case Some(T)
 case None
}

I'm not going to cover the basic USES of enumeration here, but I'm just documenting two more useful USES of enumeration.

The associated values

Correlation values are an excellent way to attach additional information to enum case.

For example, when we need to pass the values of series 1 to the next class, we usually write a few Settings like code 1 below:


struct MyStruct {
  var value: Int

  init(_ value: Int?) {
    if let val = value {
      self.value = val
    } else {
      self.value = Int(INT_MAX)
    }
  }
}

class Two {
  var value1: String?
  var value2: Int?
  var value3: MyStruct?

  func setValue1(value: String?) { }
  func setValue2(value: Int?) { }
  func setValue2(value: MyStruct?) { }
}

As the number of values to pass increases, the code will no doubt become less attractive. We can use enumerations to simplify:


enum ValueBind {
  case bindStringValue(str: String)
  case bindIntValue(num: Int)
  case bindModel(model: MyStruct)
}

class Two {
  var value1: String?
  var value2: Int?
  var value3: MyStruct?

  func setValueBind(value: ValueBind) {
    switch value {
    case .bindStringValue(let str):
      print(str)
    case .bindModel(let model):
      print(model.value)
    case .bindIntValue(let num)
      print(num)
    }
  }
}

With the enumeration of associated values, our code is immediately much cleaner.

Custom enumerated types

When we normally use enumerations, we usually use only a few basic types when defining value for enumerations:


enum Direction {
  case left
  case top
  case right
  case bottom
}

enum StringEnum: String {
  case hello = "hello"
  case world = "world"
}

enum IntEnum: Int {
  case one = 1
  case two = 2
}

However, if we need to put our custom type in the enumerated type, we need to add something to the enumeration.


enum CustomEnum: RawRepresentable {
  typealias RawValue = MyStruct

  case null
  case one
  case two
  
  init?(rawValue: MyStruct) {
    switch rawValue.value {
    case 1:
      self = .one
    case 2:
      self = .two
    default:
      self = .null
    }
  }

  var rawValue: MyStruct {
    switch self {
    case .one:
      return MyStruct(1)
    case .two:
      return MyStruct(2)
    default:
      return MyStruct(nil)
    }
  }
}

We make the enumeration comply with the RawRepresentable protocol and implement some properties and methods of the protocol:


/*
   The enumeration  RawValue  Associate for the type you want 
**/
associatedtype RawValue

/*
   Generate an instance of an enumeration with its own associated type 
**/
init?(rawValue: Self.RawValue)

/*
   The act of defining its own type  RawValue  return 
**/
var rawValue: Self.RawValue { get }

conclusion


Related articles: