Swift Chinese Handbook: enumerations

Source: Internet
Author: User
Tags integer uppercase letter

Enumeration (enumerations)

enumeration defines a common group type for values associated with a system. It also allows you to use these values in a type-safe situation while you are programming. If you're familiar with C, you know that in C, an enumeration type is a series of integer values that have a specified associated name. But it's more flexible to enumerate types in swift, and you don't have to give the enumeration Each member of the type is assigned a value. If a value (assumed to be "raw") is provided to members of all enumerated types, the value can be a string, a character, an integer, or a floating-point number. As a selection, a member of an enumeration can be specifically specified as any type of storage that is different from other members, such as a union or variant in other languages. You can define some of the associated members as a common set as part of the enumeration , each with a different set type and with the appropriate type.

Enumeration syntax

You can start with an enum and define an enumeration with curly braces that contain the entire definition:

Enum Someenumeration {  
    //define enumeration here  
}

Here's an example that defines a compass that contains four directions:

Enum CompassPoint {case, North case, South case, West  
}

The variables defined in the enumeration (as in the example north, South, east, west) are member variables (or members) of the enumeration . The keyword case is used to indicate that this line is going to define a new member variable

Attention:

Unlike C or OBJECTIVE-C, the member of the enumeration type in the swift language will not be assigned an integer value by default, in the case of CompassPoint, north, South, east, West default is not implicitly equal to 0,1,2,3. Instead, different enumeration members will be able to control what type and what value they are assigned to, and can be specified when defining CompassPoint this enumeration .

Multiple members can also be defined by a single line, separated by commas:

Enum plant{case  
    Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune  
}

Each enumeration is defined as a completely new type, just like the other types in Swfit, the name of the enumeration (like the compasspoint above, plant) should begin with an uppercase letter, giving them the singular type rather than the plural type, so that they can be self-evident:

var directiontohead = Compasspoint.west

When the Directiontohead is assigned to a possible value in CompassPoint during initialization, its type can be speculated out. Once Directiontohead is declared to be a compasspoint type, you can briefly use a comma expression to assign it to the value of the other CompassPoint:

Directiontohead =. East

The type of the directiontohead is known, so you can ignore its type to give him a value. This makes the code highly readable when using the enumeration value of the display type. To match an enumeration value with a switch statement

You can access a separate enumeration value through a switch statement:

Directiontohead =. South  
Switch Directiontohead {case  
    . North:  
        println ("Lots of the planets have a North") case  
    . South:  
        println ("Watch Out for Penguins") case  
    . East:  
        println ("Where is the Sun Rises") case  
    . West:  
        println ("Where The Skies are Blue")  
}  
//Output "Watch out for Penguins"

You can read this code like this:

Consider the value of the directiontohead if it equals. North then output "Lost of planets have a north" if it equals. South, then the output "Watch out for Penguins".

As in the chapter on control flow, when a switch statement is used to determine the enumeration value, all enumeration members must be included. Assume. West is ignored and will cause compilation errors because it does not take into account all enumerated members of the enumeration, we need to be comprehensive to ensure that all members of the enumeration are not ignored.

If it is not appropriate to consider the members of each enumeration, you can provide a default to overwrite other members that are not explicitly processed:

Let someplanet = Planet.earth  
switch someplanet {case  
    . Earth:  
        println ("Mostly harmless")  
    default:  
        println ("Don't a safe place for humans")  
}  
//Output " Mostly Harmless "

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Related Article

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.