Tenth chapter: Enumeration

Source: Internet
Author: User

Source

enumerations Define a set of related values for a common type, so that you can use them in a safe way in your code.

enumeration Syntax (enumeration Syntax)

Use the enum keyword and place their entire definition in a pair of curly braces:

enum Someeumeration {     //

Here is an example of a compass in four directions:

enum CompassPoint {       case North  case           East       Case

Multiple member values can appear on the same line, separated by commas:

enum Planet {     case

Like other types in Swift, their names (such as CompassPoint and planet) must start with an uppercase letter.

Var

The type of directiontohead is inferred when it is initialized by a possible value of compasspoint. Once Directiontohead is declared as a compasspoint, you can use the shorter point (.) syntax to set it to the value of another compasspoint:

When the type of Directiontohead is known, you can no longer write the type name when you set its value. Using enumeration values of display types can make your code more readable.

Match enumeration values and Switch statements (Matching enumeration values with a switch Statement)
 directiontohead =. South  switch   Directiontohead { Span style= "color: #0000ff;" >case  . North:println ( Lots of Planets has a north ")  case  . South:println ( Watch out for Penguins   Case  . East:println ( Where The Sun Rises ")  case  . West:println ( Where The Skies is blue " //  prints "Watch out for Penguins"  

When you do not need to match each enumeration member, you can provide a default branch to cover any member that is not explicitly raised:

Let someplanet =switch case . Earth:     println ("Mostly harmless"default:     println ("Not a safe place for Humans "//

Association value (associated values)

You can define Swift's enumeration to store any type of association value, and each member's data type can be different .

In Swift, the enumeration used to define the two commodity barcodes is this:

enum Barcode {     case  UPCA (int, int., int)     case

The above code can understand this:

"Defines an enumeration type named barcode, which can be an association value for UPCA (int,int,int), or a string-type (string) associated value for QRCode. " This definition does not provide the actual value of any int or string, it simply defines the type of the associated value.

You can then use any one of the barcode types to create new barcodes, such as:

Var

The same product can be assigned to a different type of barcode, such as:

You can extract each associated value as a constant (with a let prefix) or as a variable (prefixed with Var) in the case branch code of switch:

Switch case . UPCA (Let-numbersystem, let-identifier, let-check):     println ("upc-a with value of \ (Numbersystem), \ (identifier), \ (check). "   case . QRCode (Let ProductCode):     println ("QR code with value of \ (ProductCode)." //

Extracts all the associated values of an enumeration member:

Switch case let . UPCA (Numbersystem, identifier, check):     println ("upc-a with value of \ (Numbersystem), \ (identifier), \ (check)."   case let . QRCode (ProductCode):     println ("QR code with value of \ (ProductCode)." //

Original value (Raw values)

Here is an example of an enumeration member storing raw ASCII values:

enum Asciicontrolcharacter:character {     case Tab = "\ t" case      linefeed = "\ n"      Case Carriagereturn = "\ r"

note that the original value and the associated value are not the same. When you start defining enumerations in your code, the original values are pre-populated values like the above three ASCII codes. For a particular enumeration member, its original value is always the same. The associated value is set when you create a new constant or variable based on an enumeration member, and each time you do so, its value can be different.

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.

The following enumeration is a refinement of the previous planet enumeration, using the original integer values to represent the order of each planet in the solar system:

enum Planet:int {     case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune} 

Auto-increment means that the original value of Planet.venus is 2, and so on.

The original value of the enumeration member can be accessed using the Toraw method of the enumeration member:

Let Earthsorder =//

Use the Fromraw method of the enumeration to try to find an enumeration member with a specific original value. This example identifies Uranus by the original value 7:

Let possibleplanet = Planet.fromraw (7//

However, not all possible int values can be found for a matching planet. Because of this, the Fromraw method returns an optional enumeration member.

If you are trying to find a planet with a position of 9, the optional planet value returned by Fromraw will be nil:

if let someplanet = Planet.fromraw (positiontofind) {     switch  someplanet {       Case  . Earth:         println ("Mostly harmless")     default:         println ("not a safe Place for humans ")     Else  {     println (" There isn ' t a planet at position \ ( Positiontofind) "//

2015-03-20

15:50:56

Tenth chapter: Enumeration

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.