//Playground-noun:a Place where people can playImport UIKit//Enumeration Syntaxenumsomeenumeration {//enumeration definition goes here}//defines an enumerationenumCompassPoint { Case North Case South CaseEast CaseWest}//When you declare an enumeration, you do not assign a default value to an enumeration member like C/oc//multiple enumeration members appear on a single line separated by commasenumPlanet { CaseMercury, Venus, Earth, Mars}var directionhead=compasspoint.west//the variable directionhead is now known as the CompassPoint type, and the enumeration type can be omitted when the value is assigned againDirectionhead = . South//matching enumeration values by switchSwitchDirectionhead { Case . North:println (" North") Case . South:println (" South") Case . West:println ("West") Case . East:println ("East")default: Break}//Association value associated valuesenumBarCode { CaseUPCA (int, int, int, int)//UPCA type (int, int, int, int) CaseQRCode (String)//qrcode type is (String)}var ProductCode= BARCODE.UPCA (8,890, the, About) ProductCode= . QRCode ("ABCDEFG")//Extracting association values using let or var depends on usage within the case statementSwitchProductCode { Case . UPCA (let-A1, let-A2, let-A3, let A4): println ("upca:\ (A1)-\ (A2)-\ (A3)-\ (A4)") Case . QRCode (Let S1): println ("qrcode:\ (S1)") default: Break}//Raw Value RAW valuesenumAsciicontrolcharacter:character { CaseTab ="\ t" Caselinefeed ="\ n" CaseCarriagereturn ="\ r"}//Note: The original value and the associated value are different. For an enumeration member of an attribute, its original value is always the preset value at the time the enumeration is defined, and the associated worth is set when you create a variable or constant from an enumeration member, which can be different each time//When an integer [Ntegers] is used to initialize the original value, if the other member does not specifically specify the initial value, the original value will be self-incrementenumSomeplanet:int { CaseMercury =1, Venus, Earth, Mars, Jupiter, Santurn, Uranus, Neptune}//accessing enumeration members through the RawValue propertyLet Earthsorder =SomePlanet.Earth.rawValue//initializing an instance with raw valuesLet possibleplanet = Someplanet (rawValue:7)//The return value is optional?
Swift-8-Enumeration