The enumeration of grammar
Enum CompassPoint {// Enumerators in Swift are not assigned a default integer value when createdcaseNorth // This is the member valuecase south
case east
case west
}
Copy the code
Define enumeration members on the same line, separated by commas
Enum Planet {// Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptunecasemercury, venus, earth, mars, jupiter, saturn, uranus, // When we know a variable is an enumeration type, we can also simply use direction =.southCopy the code
Enumeration value matches
Force exhaustion, or compile fails
switch direction {
case .north:
print("this is north direction")
case .south:
print("this is south direction")
case .east:
print("this is east direction")
case .west:
print("this is west direction")}Copy the code
When using the default statement, you don’t have to do it all
switch direction {
case .north:
print("north is my favourite direction")
default:
print("I don't like other direction")}Copy the code
Traversal of an enumeration member
enum Beverage: CaseIterable {
caseCoffee, tea, juice} // Enumeration following the CaseIterablell protocolprint(beverage.allcases.count) // Iterates through all enumeratorsfor value in Beverage.allCases {
print(value)
}
Copy the code
The associated values
/** It is useful to store other types of values with member values. This additional information is the associated value */ enum Barcode {caseUpc (Int, Int, Int, Int) // The associated value is of type (Int, Int, Int, Int)caseQrCode (String) // The type of the relationship value is (String)} var productBarcode = barcode. upc(8, 88888, 66666, 3)print(productBarcode)
productBarcode = Barcode.qrCode("thisisgood")
print(productBarcode)
Copy the code
Check for different barcode types with Switch
switch productBarcode {
// case .upc(let systemNumber, let manufacturer, let product, let check):
// caseVar.upc (systemNumber, manufacturer, product, check): //case let. Upc (systemNumber, manufacturer, product, check): // You can also do thisprint("UPC: \(systemNumber) - \(manufacturer) - \(product) - \(check)")
case .qrCode(let code):
print("QR code: \(code)")}Copy the code
Original value/Default value
/** The type of the original value must be the same for a particular enumeration member, whose original value remains the same */ enum ASCIIControlCharacter: Character, CaseIterable {case tab = "\t"/ / TABcase lineFeed = "\n"/ / a newlinecase carriageReturn = "\r"/ / enter} var c = ASCIIControlCharacter. TABCopy the code
Implicit assignment of the original value
/** When an integer is used as the original value, the implicit assignment value is successively incremented by 1, default original value is 0 */ enum Star: Int {// Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune // The explicit primitive value of meycury is 1, then the implicit primitive value of Vernus is 2, and so oncase mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
Copy the code
// the rawValue property of the enumerator accesses the rawValueletStarValue = star.earth. rawValue // starValue Implicit type: Intprint(starValue)
Copy the code
/** When a String is used as the raw value of an enumeration type, the implicit raw value of each enumeration member is the name of the enumeration member */ enum Compass: String {case north
case south
case east
case west
}
letDirectionValue = Compass.south. RawValue // directionValue Implicit type: Stringprint(directionValue)
Copy the code
Initialize the enumeration instance with the original value
// possibleStar is of type Star? Not all ints can find a matching planetlet possibleStar = Star(rawValue: 10);
if let someStar = possibleStar {
print(someStar)
} else {
print("There isn't a star")}Copy the code