Directory:
- Basic syntax
- Associated value
- Original value
Enumerations define a common type for a set of related values.
? You can specify the original value type for an enumeration member: string, character, integer value, or floating-point number, etc.
An enumeration member can specify that an association value of any type is stored in an enumeration member
Enumeration can define constructors and follow protocols
? Enumeration is a value type
Basic syntax
enum CompassPoint { Case North Case South CaseEast CaseWest}//multiple member values can appear on the same lineenum Planet { CaseMercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}var directiontohead= Compasspoint.west//defining enumeration variables and assigning valuesDirectiontohead =. East//Assigning a value to an enumeration variable of a known type//using enumerations in SwitchLet someplanet =Planet.earthswitch someplanet { Case. Earth:print ("Mostly Harmless") Default:print ("Not a safe place for humans")}
Associated value
You can define an association value of any type for an enumeration, and each associated value type can be different.
enum Barcode { CaseUPC (int, int , int, int) CaseQRCode (String)}var Productbarcode= BARCODE.UPC (8,85909,51226,3)//Assigning BarcodesProductbarcode =. QRCode ("Abcdefghijklmnop")//can also be assigned a value of two-dimensional code//extracting enumeration associated valuesSwitch 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).")。//Print Output}//all associated values of an enumeration member are extracted as constants or variables, using let or VarSwitch Productbarcode { CaseLet . UPC (Numbersystem, manufacturer, product, check): Print ("UPC: \ (numbersystem), \ (manufacturer), \ (product), \ (check).") CaseLet . QRCode (ProductCode): Print ("QR code: \ (productCode).") }
Original value
The original value is different from the associated value, and the original value is a fixed, immutable value that is prepopulated when the enumeration is defined, and the associated value is a mutable value that is set only when the enumeration constant or variable is created.
//the original value of the first element is assigned a value of 1, and then the element value incrementsenum Planet:int { CaseMercury =1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}//Compasspoint.south has an implicit primitive value Southenum Compasspoint:string { CaseNorth , south, east, West}//use the enumeration member RawValue property to access its original valueLet Earthsorder =Planet.earth.rawValue//to create an optional enumeration constant using the enumeration primitive valuesLet possibleplanet = Planet (rawValue:7)//possibleplanet type is Planet? value is Planet.uranus
Statement: This series of content is from the network or electronic books, only to do learning summary!
Swift Learning Note (8): Enumeration