Enumeration types for Swift 3.0 basic learning
- 2020-05-24 06:18:02
- OfStack
The enumeration of grammar
Define an enumeration using the keyword enum
enum SomeEnumeration {
// enumeration definition goes here
}
For example, a compass has four directions:
enum CompassPoint {
case north
case south
case east
case west
}
Unlike c and objective-c, Swift's enumeration members are not given a default integer value when they are created. So instead of zero through three in the above code, the different enumerated types are themselves fully fledged values with a well-defined CompassPoint type.
It can also be declared in the same line:
enum Planet {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
Enumeration assignment:
var directionToHead = CompassPoint.west
1 when directionToHead is explicitly a variable of type CompassPoint, the point syntax can be used to assign the value:
directionToHead = .east
The enumeration value of the Switch expression matches
The expression switch is as follows:
directionToHead = .south
switch directionToHead {
case .north:
print("Lots of planets have a north")
case .south:
print("Watch out for penguins")
case .east:
print("Where the sun rises")
case .west:
print("Where the skies are blue")
}
// Prints "Watch out for penguins"
Of course, default can also be added here to satisfy all situations:
let somePlanet = Planet.earth
switch somePlanet {
case .earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
// Prints "Mostly harmless"
The associated values
In Swift, enumeration is used to define a product barcode:
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
Can understand the code above: define a called Barcode enumerated types, with a value type (Int Int, Int, Int) upc and value type (String)
You can now create one of these types of bar codes by:
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
The barcode of another type of the same product can be assigned by:
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
You can use switch to view two different barcode types:
enum CompassPoint {
case north
case south
case east
case west
}
0
The above can also be changed to:
enum CompassPoint {
case north
case south
case east
case west
}
1
The original value
Here is an enumerated type that holds the original ASCII value:
enum CompassPoint {
case north
case south
case east
case west
}
2
Similar to the above associated values, you can specify the default value (raw values) for each case in the enumeration.
It is important to note that the original value is not the same as the associated value. The original value was set the first time the enumeration code was defined, and the original value of all enumeration case of the same type is the same. The associated value is set when you create a new constant or variable based on the enumeration case, and you can use a different value every time you create it.
Implicitly assigned original value
If the original value of case in the enumeration is an integer or a string, you do not need to assign the original value to each case; Swift will assign the value for you automatically.
Such as:
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
Planet.mercury has an implicit original value of 1, Planet.venus has an implicit original value of 2, and so on.
If the enumeration's original value is of type string, then its original value is the text of the case name, for example:
enum CompassPoint {
case north
case south
case east
case west
}
4
Initializes from the original value
If you define an enumeration of a primitive value type, the enumeration will automatically create an initializer with the primitive value type (parameter name rawValue), for example:
enum CompassPoint {
case north
case south
case east
case west
}
5
Not all Int values can be found corresponding to planet, so the original value initializer returns an enumeration of optional, possiblePlanet in the above example is Planet? Type.
If you want to find planet with the original value of 11, the initializer will return nil:
enum CompassPoint {
case north
case south
case east
case west
}
6
Recursive enumeration
A recursive enumeration is an enumeration that contains one or more enumeration instances of the associated value case, using the keyword indirect to indicate that an enumeration case is recursive.
For example, here is an enumeration that holds a simple algorithm expression:
enum CompassPoint {
case north
case south
case east
case west
}
7
You can also write indirect directly before the enumeration definition:
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}
The following code is an example of how to create this recursive enumeration:
enum CompassPoint {
case north
case south
case east
case west
}
9
Applied to calculation functions:
func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case let .number(value):
return value
case let .addition(left, right):
return evaluate(left) + evaluate(right)
case let .multiplication(left, right):
return evaluate(left) * evaluate(right)
}
}
print(evaluate(product))
// Prints "18"
The algorithm expression for the example above is :(5 + 4) * 2, resulting in 18
Reference:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-ID145
conclusion