Enumeration in the C language, the value of an enumeration can only be a number, and in swift the value of the enumeration is called raw value and can be represented by string, Character, Int, float, and so on.
Enum MyEnum {
}
For example: The enum seasons {case Spring case, Summer case, Autumn case Winnter} or can also be written as: enum seasons { Case Sprint, Summer, Autumn, Winnter}
After the enumeration is defined, the name of the enumeration is the other type class (Int, String ...) that we have seen before. Like, it can be used like any other type, so in the official document, when you define an enum, the first letter of the enum type is capitalized, such as the first s of the seasons defined above is uppercase.
Since it is a type, then we can define the variable with an enum:
var Myseason = seasons.spring
When the Myseason is initialized (at which point the Myseason type is seasons), the enumeration value can be assigned directly without the need to write the enumerated type: Myseason =. Winnter//Lifting value front "." Can not be omitted
It is similar in switch:
Switch Myseason {case. Spring:println ("Chun") case. Summer:println ("Xia") case. Autumn:println ("Autumn") case. Winnter:println ("Winter") default:println ("The season assignment is wrong.") ") }
When all the values of an enum are listed in the Switch case, the default is no longer required to write and Xcode will not be prompted, but if it is not written in full and the default is not written, Xcode will prompt "Switch must be exhaustive, Consider adding a default clause "
You can also let the value of an enum be a custom type (an official example): enum Barcode {case UPCA (int, int, int.) Case QRCode (String)}
Note, however, that in the above definition, barcode does not store any int and string, but simply defines a type, and we still need to define an additional variable to store the required information: var Productbarcode = BARCODE.UPCA (8, 85909_51226, 3)
Or use a two-dimensional code to denote this productbarcode variable: productbarcode =. QRCode ("ABCSDFGQWERQWF")
The Productbarcode variable is actually a barcode type (the one that is defined above), so you can assign it to him. UPCA or. QRCode
In the section "Control flow," We saw the use of let in Swith's case, and we can use the Let method for the switch to barcode this enum:
Switch Productbarcode {case. UPCA (Let-numbersystem, let-identifier, let-check)://Here you can do operation case for Numbersystem, identifier, check. QRCode (Let ProductCode):///Here you can do the operation of the ProductCode}//mentioned above, switch, if all the case has been listed, you do not need to use the default
In the Control Flow section, we also mentioned that if all the values in the case are variables, you can write let in the front:
Switch Productbarcode {case let. UPCA (Numbersystem, identifier, check)://This can be done to Numbersystem, identifier, check to do the operation case let. QRCode (ProductCode)://Here can do the ProductCode operation}
Raw values can define values for an enum, called raw values, which can be basic types such as int, String, double, and so on:
Enum Planet:int {Case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}
At this point, Planet the raw value of this enumeration is an integral type, and as soon as a value is given, the raw value of the subsequent values is incremented one at a time: Planet.venus's raw value is 2, and Planet.earth's raw value is 3 ...
var earthorder = Planet.earth. Toraw ()//Note Toraw () This method can take the raw value of the enumeration value
You can also get an enumeration value from raw value: var whichplanet = Planet. Fromraw (2)//note Fromraw () This method is followed by the enumeration type, unlike the Toraw ()
It is worth noting that, according to the above definition, if we use Planet.fromraw (100000) to get the value of what. Obviously this value is not defined, so the Fromraw () method, the return value is optional, so whichplanet is the type of the variable Planet? or optional Planet.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service