1. In the context of Swift and Java about enumerations, two language grammars are similar.
Swift defines enumerations:
enum compasspoint{ case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune }
Java Definition enumeration:
Public enum ColorSelect { red, green, yellow, blue; }
2. Enumeration and switch use together
The SWIFT code is as follows:
enumcompasspoint{ CaseMercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune} let Directiontohead=Compasspoint.venusSwitchDirectiontohead { CaseCompassPoint.Earth:print ("This is \ (directiontohead)") CaseCompassPoint.Venus:print ("This is \ (directiontohead)") CaseCompassPoint.Mercury:print ("This is \ (directiontohead)") CaseCompassPoint.Jupiter:print ("This is \ (directiontohead)") }
The 2.1 Case switch
Branch code extracts each correlation value as a constant ( let
prefixed) or as a variable ( var
prefixed) to use: The code is as follows:
enumBarcode { CaseUPCA (int, int, int) CaseQRCode (String)} var productbarcode= BARCODE.UPCA (8, 85909_51226,3) Productbarcode= . QRCode ("Abcdefghijklmnop") SwitchProductbarcode { CaseLet . UPCA (Numbersystem, identifier, check): Print ("upc-a with value of \ (Numbersystem), \ (identifier), \ (check).") CaseLet . QRCode (ProductCode): Print ("QR code with value of \ (ProductCode).") } //output "QR code with value of Abcdefghijklmnop."
2.2 Raw VALUES: Enumeration members can be prepopulated by default values (known as raw values), where the original values have the same type.
enum Asciicontrolcharacter:character { case"\ t"case "\ n"case "\ R"}
The original value can be a string, a character, or any integral or floating-point value. Each original value must be unique within its enumeration declaration. When an integer value is used for the original value, the other enumeration members are automatically incremented if they do not have a value.
enum Planet:int { case1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}
The properties of an enumeration member rawValue
can be used to access the original value of the enumeration member and to create an enumeration of the rawValue
specific original values for the constructor by argument. The code is as follows:
Let Earthsorder = Planet.Earth.rawValue// Earthsorder is 37) possibleplanet is of type Planet? and equals Planet.uranus
However, not all possible Int
values can be found for a matching planet. Because of this, the constructor can return an optional enumeration member. In the example above, possiblePlanet
it is the Planet?
type, or "optional Planet
".
If you are trying to find a planet with a position of 9, the optional value returned by the parameter for the rawValue
constructor Planet
will be nil
:
enumPlanet:int { CaseMercury =1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune} let Positiontofind=9 ifLet someplanet =Planet (rawvalue:positiontofind) {Switchsomeplanet { Case . Earth:print ("Mostly Harmless") default: Print ("Not a safe place for humans") } } Else{print ("there isn ' t a planet at position \ (positiontofind)") }
This example uses an optional binding (optional binding) to 9
attempt to access a planet by its original value. if let somePlanet = Planet(rawValue: 9)
statement to obtain an optional Planet
, if optional Planet
can be obtained, set to somePlanet
the optional Planet
content. In this example, the location 9
of the planet cannot be retrieved, so the else
branch is executed.
Comparison of the enumeration between Swift and Java