How to use the swift enumeration to define
This article focuses on "how to use swift enumeration definition", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to use swift enumeration definition"!
Enumerated definition / / enumerated definition enum CompassPoint {case north case south case east case west} / / often used with switch case to use let directionToHead = CompassPoint.southswitch directionToHead {case .north: print ("Lots of planets have a north") case .south: print ("Watch out for penguins") case .east: print ("Where the sun rises") case .west: print ("Where the skies are blue")} / * Watch out for penguins * / enumeration traversal / / enumerate traversing enum Planet: CaseIterable {case mercury Venus, earth, mars, jupiter, saturn, uranus, neptune} for planet in Planet.allCases {print (planet)} / * mercury venus earth mars jupiter saturn uranus neptune * / related values / / related values enum Barcode {case upc (Int, Int) case qrCode (String)} func showBarcode (_ barcode: Barcode) {switch barcode {case .upc (let numberSystem, let manufacturer, let product, let check): print ("UPC:\ (numberSystem),\ (manufacturer),\ (product)) \ (check) ") case .qrCode (let productCode): print (" QR Code:\ (productCode) ")} var productBarcode = Barcode.upc (1,1,1,1) showBarcode (productBarcode) / / UPC:1, 1,1 1productBarcode = Barcode.qrCode ("hello") showBarcode (productBarcode) / / QR Code: hello original value / / original value enum ASCIIControlCharacter: Character {case tab = "\ t" case lineFeed = "\ n" case carriageReturn = "\ r"} initialize / / initialize enum RoleStatus: Int from the original value CaseIterable {case run case jump case walk case idle} for i in 0...RoleStatus.allCases.count {print (RoleStatus (rawValue: I))} / * Optional (_ _ lldb_expr_8.RoleStatus.run) Optional (_ _ lldb_expr_8.RoleStatus.jump) Optional (_ _ lldb_expr_8.RoleStatus.walk) Optional (_ _ lldb_expr_8.RoleStatus.idle) nil * / Recursive enumeration (indirect) / / Recursive enumeration (5x4) * 2indirect enum ArithmeticExpression {case number (Int) case addition (ArithmeticExpression ArithmeticExpression) case multiplication (ArithmeticExpression, ArithmeticExpression)} let five = ArithmeticExpression.number (5) let four = ArithmeticExpression.number (4) let sum = ArithmeticExpression.addition (five, four) let product = ArithmeticExpression.multiplication (sum, ArithmeticExpression.number (2) func evaluate (_ expression: ArithmeticExpression)-> Int {switch expression {case let .number (value): return value case let .replication (left, right): return evaluate (left) + evaluate (right) case let .multiplication (left) Right): return evaluate (left) * evaluate (right)}} print (evaluate (product)) / / 18 so far I believe that you have a deeper understanding of "how to use the swift enumeration definition", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!