Swift Learning Note 10

Source: Internet
Author: User
Tags integer numbers

Enumeration

An enumeration defines a common type for a set of associated values, and allows you to manipulate these values in the code type safely.

The enumeration in C assigns the associated name to a series of integer values. The enumeration types in Swift are more lively, and there is no need to specify a value for each member, and if the value is specified (raw value), the value can be either string or character, integer, or floating-point.

In addition, each enumeration member can specify a different number of associated values for any type to store. It's kind of like a union in other languages. You can define a common set of associative values as part of an enumeration, each of which can have a different set of appropriate types.

In swift, enum types are first class, and they have many attributes that were previously only owned by the class, such as attributes that provide additional information to the current value of the enumeration, and an instance method that provides operations for the value represented by the enumeration. Enumeration types can also provide initialization methods to provide initialization member values, or they can be extended to increase operations outside of the original operation, and can also comply with protocols to provide standard operations.

Enumeration syntax

Create an enumeration with the Enum keyword, and place all the definitions inside a pair of curly braces:

enum       compasspoint.           East    case  West}

The case keyword implies that the new member value of a row is to be defined, unlike the C language, where the enumeration members in Swift are not assigned an integer value at the time of creation, and in the above example, North is not implicitly equal to 0, they are all perfectly valid values. is a clearly defined data type, CompassPoint.

The same case row can have multiple values separated by commas.

Like other data type names in SWIFT, enumerated names should start with uppercase letters.

When an enumeration member is assigned to a variable, the data type of the variable can be inferred as the enumeration type, and the value can then be changed with the abbreviated point syntax:

var directiontohead ==. East

To match an enumeration value with a switch statement:

Directiontohead = . SouthSwitchDirectiontohead { 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"

As described in the previous control flow, when you use a switch statement to handle an enumeration type, it must be comprehensive, such as if the case is omitted. West, you cannot compile because the switch statement does not take into account the full enumeration member. This prevents the enumeration member from being accidentally omitted. Of course, a default branch can be used to handle all enumeration members without overwriting them.

Associated value

In the previous example, the enumeration member is itself the value itself. In addition, the variable/Chang value can be given to the enumeration member and then used elsewhere. However, it is sometimes necessary to rely on these member values to store some other types of associated values, which allows you to store additional custom information by relying on these member values, and allows this information to be different each time a member is used.

You can define an enumeration to store the associated values of any data type that is already defined, and the associated values for each enumeration member can be of different types. For example, the enumeration type that defines the inventory code, which has two members, one that represents a one-dimensional barcode (which can always be identified with 4 integer numbers), and one that represents a QR code (which can always be identified by a string):

enum Barcode {    case  UPCA (int, int, int, int.)    case  qrcode (String)}

Here, the enumeration members have their own associated values, and when defined, they only need to indicate the type of the associated value, not the given value. In this way, the new barcode type variable can be created as:

var productbarcode = BARCODE.UPCA (885909512263)

A variable is created here, and the value assigned to it is a useful barcode.upca of the associated tuple (8, 85909, 51226, 3), after which the variable can be assigned to a different barcode type value, such as:

Productbarcode =. QRCode ("abcdefghijklmnop")

The different barcode types can still be checked in the switch statement as before, and this time the correlation value can be taken out as part of the switch statement. You can remove an association value as a constant or variable for use inside a case body:

Switch Productbarcode { case . UPCA (Let-numbersystem, let-Manufacturer, let-product, let-check):    println ("upc-a: \ (numbersystem ), \ (manufacturer), \ (product), \ (check). " ) case . QRCode (Let ProductCode):    println ("QR code: \ (productCode). " )}//  prints "QR code:abcdefghijklmnop."

If all of the associated values are removed as constants/variables, you do not need to write the var/let before each associated value, referring to the previous keyword:

Switch Productbarcode { case let . UPCA (numbersystem, manufacturer, product, check):    println ("upc-a: \ (numbersystem), \ ( Manufacturer), \ (product), \ (check). " ) case let . QRCode (ProductCode):    println ("QR code: \ (productCode). " )}//  prints "QR code:abcdefghijklmnop."

Original value (Raw values)

As mentioned earlier, enumeration members can define different types of association values. In addition, you can enumerate members or give default values (known as raw value), which must be of the same type. Like what:

enum Asciicontrolcharacter:character {    case"\ t"     case " \ n "     Case " \ r " }

Note the difference between the original value and the associated value, the original value is given at the time the enumeration type is defined, and is always consistent, and the associated value is not given when the enumeration type is defined, but the constant or variable value is given when the enumeration type variable is created.

The original value can be a string, a character, an integer, a floating-point number. Each original value must be unique within the enumeration declaration. If the original value is shaped, the system automatically assigns the value to the member if it is not assigned the original value. Like what:

enum Planet:int {    case1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}

The system automatically assigns the original value to subsequent members in turn. The initial value of a member can be obtained through rawvalue, such as:

Let Earthsorder = Planet.Earth.rawValue//  Earthsorder is 3

If you define an enumeration type that has an original value type, the enumeration automatically gets an initialization method that accepts the original value type parameter and returns an enumeration member or nil, which you can use to create an enumeration instance:

7 )//  possibleplanet is of type Planet? and equals Planet.uranus

Not all integers can find a matching member within the enumeration, so this initialization method returns an optional enumeration member (optional enumeration member).

Swift Learning Note 10

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.