Enumeration types for Swift programming

  • 2020-05-13 03:33:17
  • OfStack

Must have written the procedure of the children's shoes is not strange to enumerated types, the use of enumerated types is a lot of benefits, here do not do too much to describe. Enumerations in the Fundation framework and UIKit are even more numerous, making your code easier to read and more maintainable. Enumerations in the Swift language are pretty impressive. Enums in Swift not only retain the features of enums found in most programming languages, but also add some new features that are easy to use and useful. In this article, you'll see the magic of enums in Swift.

There are small partners will ask, is not enum, what to say. What about enums in Swift? Enums in Swift are much more powerful. They can not only bind values to enum elements, but also associate multiple values to enum elements, assign an enum value to an enum variable through the value of the element, and enum functions can be defined in the enum. Next, we'll start looking at the enumeration in Swift below 1.

1. Define an enumeration

The definition of enumeration in Swift is different from that in other programming languages. Each enumeration element is preceded by the keyword case, and case is followed by the enumeration element. The following are two ways to define enumeration types.

1. Multiple case, with an case before each enumeration element


// Definition of enumeration 
enum KindOfAnimal {
 case Cat
 case Dog
 case Cow
 case Duck
 case Sheep
} 

2.1 case handle all elements, separated by commas between enumeration elements


 // You can also define enumerated types in this way 
enum KindOfAnimalTwo {
 case Cat, Dog, Cow, Duck, Sheep
 }

2. Use of enumeration types

After defining the enumeration type, it is for the purpose of using it, right? You can declare variables directly by using the enumeration type. In Swift, typedef is not needed to define the enumeration type, we can directly use the enumeration type.


// Define enumeration variables and assign values 
var animal1: KindOfAnimal = KindOfAnimal.Cat

You can also omit the enumeration type name when assigning an enumeration variable, because the type of the enumeration variable is specified when the enumeration variable is declared.


var animal2: KindOfAnimal = .Dog 

Use our enumeration variable in Switch


// in Switch ... Case Using enumeration in 
switch animal1 {
 case KindOfAnimal.Cat:
 println("Cat")
 case KindOfAnimal.Dog:
 println("Dog")
 case KindOfAnimal.Cow:
 println("Cow")
 case KindOfAnimal.Duck:
 println("Duck")
 case KindOfAnimal.Sheep:
 println("Sheep")
 default:
 println("error =  Ha ha ")
}

3. Assign values to enumeration members

When enums are declared in Swift, it is possible to assign one value to each enum member, and the following City enum member is assigned one value, as shown below:


// Assign a value to an enumeration 
enum City: String{
 case Beijing = " Beijing "
 case ShangHai = " Shanghai "
 case GuangZhou = " Guangzhou "
 case ShengZhen = " shenzhen "
}

Using the rawValue enumeration variable, you can get the value assigned to each enumeration member as follows:


// Define enumeration variables and assign values 
var myInCity: City = City.Beijing

// Gets the value of the enumeration variable 
var myInCityString: String = myInCity.rawValue;
println(myInCityString) // Output: Beijing 

4. Assign values to enumeration variables by the values of the enumeration members

What is assigning a value to an enumeration variable by the value of an enumeration member? For example, the above enumeration type City is taken as an example. If we only know that the value of one enumeration member is "Beijing", but do not know that the enumeration member corresponding to the value "Beijing" is "Beijing", we can assign one enumeration member "Beijing" to the enumeration variable through the value "Beijing" in Swift.

Here's the code for assigning values to enumeration variables by the original values of the enumeration members


// Assign a value to an enumeration member by its value 
var youInCity: City? = City(rawValue: " Beijing ");

Why is our youInCity an optional value type? The reason is very simple, we are not sure whether the value of the enumeration City member contains "Beijing", the enumeration variable youInCity is not sure, so it is of optional type. Next, we will take out the value of youInCity, and first judge whether youInCity is City.Beijing.


// Take out the youInCity The values in the 
if youInCity == City.Beijing {
 var cityName: NSString = youInCity!.rawValue
 println(cityName) // Output: Beijing 
}

Find a string not included in the value of an enumeration member to assign value to the enumeration variable. Observe the result of 1. The value of testCity below is printed as nil, because no member in the enumeration has a value of "jing".


// The incoming 1 A value not found in an enumeration 
var testCity: City? = City(rawValue: " Beijing ");

// testCity for nil
println(testCity)

5. Enumeration value self-increment

Good thing to keep, if the enumeration value in Swift is an integer, the one after the first assignment will automatically increase. I won't say much about enumeration value augmenting, just look at the example.


 // You can also define enumerated types in this way 
enum KindOfAnimalTwo {
 case Cat, Dog, Cow, Duck, Sheep
 }
0

6. Enumerate associated values

What are the associated values of an enumeration? It literally associates a value to an enumeration member, which is exactly what happens when you assign a value to an enumeration variable. How do you do that in Swift? You use parentheses to specify the type of the associated value when you declare an element in an enumeration type, and then you associate one or more values when you assign a value to an enumeration variable, looking directly at the instance.

The following code specifies two associated values of type String for iOS, which are associated when assigning values to enumeration variables. Associating these two values can be used in an Switch statement.


 // You can also define enumerated types in this way 
enum KindOfAnimalTwo {
 case Cat, Dog, Cow, Duck, Sheep
 }
1

7. Enumeration functions

Enums in Swift can be used to add functions. The following code snippet adds a description function on the basis of the above associated value code, and returns the enumeration information of the current enumeration variable, as shown in the following code snippet:


 // You can also define enumerated types in this way 
enum KindOfAnimalTwo {
 case Cat, Dog, Cow, Duck, Sheep
 }
2

That is the end of this article, I hope to help you learn swift software programming.


Related articles: