“This is the 27th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Creation of an enumeration type

In Swift, enumerations are created using the enum keyword. For example, we can create an enumeration type for flowers:

We use case for enumeration type definitions. In the above code, we use a case for each definition. We can also define multiple enumerations in a case:

The use of enumerated values

When used, like any other type, we can declare a variable with its type specified as an enumerated type, or have the compiler automatically infer the type of the variable through variable initialization.

In Swift, if the variable is identified as an enumeration type, the enumeration type can be omitted and the point syntax is used directly, as follows:

We define two variables f1 and F2 in two ways. They are both enumerations of Flowers. The difference is that F1 first declares the variable and then assigns it a value, while F2 directly assigns an initialized value to the variable and lets the compiler determine its type automatically.

Note that since F2 must tell the compiler which enumeration type it is when it initializes, it cannot omit enumeration types:

In real development, enumeration types are often used in conjunction with switch-case to implement selection, as follows:

Note that enumeration values need to be exhausted in switch, otherwise an error will be reported;

The raw value of the enumeration

The primitive value feature of an enumeration allows you to bind an enumerated value to another data type, and the related value can associate some other data with the enumerated value.

The original value

In C and OC, enumerations are supported by integers by default, which means that A, B, and C default to 0,1,2, respectively:

Enumerations in Swift are more flexible and do not need to supply a value to every member of the enumeration. If a value is to be supplied to every member of the enumeration, it can be a string, a character, a floating-point type, etc.

As follows, we give the enumeration a primitive value type:

We give Flowers a String and assign a primitive value to the enumerated value.

If we are to the original value of the specified enumeration type is an Int type, you can only set up the first to the original value of the enumeration values, its back to the original value of the enumeration values will be in the first enumeration values on the basis of the original values increase one memory, this belongs to the implicit RawValue distribution, its establishment in the Swift type inference mechanism, the following code:

We can get the rawValue of the enumeration by using the rawValue property in the enumeration type. Let’s look at the following code:

Using the rawValue attribute, we get the rawValue of the enumeration. We also find that for String enumerations, the system defaults to a rawValue of the same name for each enumeration. We can analyze it through the SIL file:

When we access rawValue, we actually access its get method. We use the following code to analyze the process:

After generating the SIL file, we analyze its call:

In the call StringValue. RawValue. Getter method, the essence of which is called the apply 6 (5) % %, % 5 is StringValue. A! Enumult, and the corresponding method %6 can be implemented as follows:

Match the value passed to StringValue because we passed stringValue.a! Case # stringValue.a! Enumult: bb1, then the corresponding bb1 method will be executed:

We can see that bb1 gets a string constant a directly, whereas normal constants are stored in the MachO cString:

A string is directly found in the Cstring table by address, this process can be verified by assembly code;

Initializers can fail

In SIL we see from the definition of enum that there is a failable initializer init, okay? (rawValue: String), that is, we can use this method to construct an enumeration variable:

var aaa = StringValue.init(rawValue: "a")
Copy the code

It is important to note that when constructing an enumeration instance from a raw value, it is possible to fail because the raw value we pass in does not necessarily correspond to an enumeration value. So this method returns an Optional value of type Optional, or nil if construction fails:

The rawValue of the original value is stored in a contiguous memory space. Init (rawValue:) is called to find the corresponding string from a contiguous array