This is the 9th day of my participation in Gwen Challenge
The enumeration of grammar
-
Use the enum keyword to introduce enumerations and put their entire definition in {}
-
Multiple cases can be displayed on multiple or one line, with one line of display separated by commas (,)
-
Enumeration names begin with a capital letter
enum Season { case spring case summer case autumn case winter } enum Season { case spring,summer,autumn,winter } Copy the code
Note: The Swift enumeration is created without assigning default integer values. Spring, summer, autumn and winter do not equal 0,1,2,3. Instead, the values are of a well-defined Season type
-
Use enumerated
When the type of the season is known, assigning to it again can omit the enumeration type name
enum Season { case spring,summer,autumn,winter } var season = Season.spring print(season) //spring season = .summer print(season) //summer Copy the code
Enumerations are value types
enum Season {
case spring,summer,autumn,winter
}
let likeSeason = Season.summer
var currentSeason = likeSeason
currentSeason = .winter
print(likeSeason)
print(currentSeason)
log:
summer
winter
Copy the code
Because Season is an enumeration, the currentSeason value is actually a copy of likeSeason, not the currentSeason itself, because they are two completely different instances. Log proof that changes to either currentSeason or likeSeason will not affect the other
Use enumeratedswitch
Statement matches enumeration items
When determining the value of an enumerated type, the switch statement must override all cases, and ignoring one will fail compilation
var season = Season.spring
switch season {
case .spring:
print("spring")
case .summer:
print("summer")
case .autumn:
print("autumn")
case .winter:
print("winter")}Copy the code
Enumeration iteration
Using CaseIterable after the enumeration name allows the enumeration to have a collection of all enumerations that can be retrieved by calling the allCases property of the enumeration type
enum Season:CaseIterable {
case spring,summer,autumn,winter
}
let count = Season.allCases.count
print(Total number of enumeration items \(count))
for season in Season.allCases {
print(season)
}
log: Enumeration item total4Spring, summer, autumn, winterCopy the code
Enumeration associated value
-
Enumerations in Swift can store associative values of any determined type, which are called enumerated associative values
Set associative values for an enumeration where one member is of type (Int, Int, Int) and the other is of type associative values of type String,String. Create a variable named Code and assign a value to it
enum Code{ case num(Int,Int,Int) case str(String.String)}var code = Code.num(2.3.3) code = .str("A"."B") Copy the code
-
Use the switch statement to check for different types
Matches enumeration items and extracts the associated value of an enumeration item. You can use let and var to extract each associated value as a constant or variable for use in the body of the case.
var code = Code.num(2.3.3) code = .str("A"."B") switch code { case .num(let num1, let num2, let num3): print("\(num1),\(num2),\(num3)") case .str(let str1, let str2): print("\(str1),\(str2)")}switch code { case let .num(num1,num2,num3): print("\(num1),\(num2),\(num3)") case let .str(str1,str2): print("\(str1),\(str2)")}Copy the code
The original value
Enumerators can set primitive values, which must be of the same type
enum Season:String {
case spring = "A"
case summer = "B"
case autumn = "C"
case winter = "D"
}
Copy the code
Note: The original and associated values are different. Raw values are the values that were prepopulated when the enumeration was defined. For a particular enumerator, its original value is always the same. An association value is a value that is set when creating a constant or variable based on an enumerator, whose association value can change.
Implicit assignment of the original value
- When an integer is used as the original value of an enumerator, the implicit assignment is incremented by one
enum Season:Int {
case spring = 1
case summer
case autumn
case winter
}
Copy the code
- When a string is used as a primitive value for an enumeration type, the implicit primitive value for each enumeration member is the name of that enumeration member
enum Season:String {
case spring
case summer
case autumn
case winter
}
Copy the code
- The rawValue of an enumerator can be accessed using the rawValue property of that enumerator
print(Season.spring.rawValue)
Copy the code
- Initialize the enumeration instance with the original value
You can initialize an enumerator with rawValue, and the return value is an enumerator or nil and that tells you if this new enumerator is in the enumeration value
enum Season:Int {
case spring
case summer
case autumn
case winter
}
if let season = Season(rawValue: 5){
switch season{
case .spring:
print("spring")
case .summer:
print("summer")
case .autumn:
print("autumn")
case .winter:
print("winter")}}else{
print("Cool")}Copy the code
Recursive enumeration
- Recursive enumeration is a type of enumeration
- There is one or more enumerators that use the enumerator type as an enumerator
- To the enumerator
indirect
To indicate that the member is recursive
enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
Copy the code
- application
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}
func evaluate(_ expression:ArithmeticExpression) -> Int {
switch expression {
case let .number(value):
return value
case let .addition(value1, value2):
return evaluate(value1) + evaluate(value2)
case let .multiplication(value1, value2):
return evaluate(value1) - evaluate(value2)
}
}
let num = ArithmeticExpression.number(5)
print(evaluate(num)) / / print "5"
print(evaluate(ArithmeticExpression.addition(num, num))) / / print "10"
print(evaluate(ArithmeticExpression.multiplication(num, num))) / / print "0"
Copy the code