This is the 8th day of my participation in the First Challenge 2022. For details: First Challenge 2022.
- This article mainly introduces the knowledge of enumeration in SWIFT
1. Basic use of enumerations
Enumeration use in 1.1 OC
Recall that when we define enumerations in OC or C, the first argument to NS_ENUM is the type and the second argument is the name of the enumeration
/// Traffic congestion
typedef NS_ENUM(NSInteger, MATrafficStatus)
{
MATrafficStatusSmooth = 1./ / / < 1
MATrafficStatusSlow, / / / < 2 go slow
MATrafficStatusJam, / / / < 3 blocks
MATrafficStatusSeriousJam, ///< 4 Severely blocked
};
Copy the code
Or we can name the method at the end and not specify the type, depending on the type we defined.
typedef enum {
AFEncapsulationBoundaryPhase = 1,
AFHeaderPhase = 2,
AFBodyPhase = 3,
AFFinalBoundaryPhase = 4,
} AFHTTPBodyPartReadPhase;
Copy the code
If the enumeration default is not set, the default value for the first enumerator is an integer of type 0, followed by recursive values
Typedef enum: NSInteger {MyEnumValueA,/ / the default 0
MyEnumValueB,
MyEnumValueC,
} MyEnum;
Copy the code
1.2 Use of enumeration in SWIFT
enum week {
case MON
case TUE
case WED
case THU
case FRI
}
Copy the code
The equivalent of
enum Weak{ case MON, TUE, WED, THU, FRI }
Copy the code
We can also specify a type like Int
enum week:Int {
case MON = 1
case TUE
case WED
case THU
case FRI
}
Copy the code
String
type
enum week:String {
case MON = "On Monday"
case TUE = "Tuesday"
case WED = "On Wednesday"
case THU = "Thursday"
case FRI = "Friday"
}
Copy the code
If we don’t want to write, we can assign RawValue as the default implicit value, which is what our current case value is.
If you change it, you don’t use the default value
Int
The default value for type
After modification, the previous values will use the default values, and the subsequent values will be accumulated according to the values we pass in.
2. The associated value of the enumeration
If we want to express complex types with enumerations, we need to use associative values. For example, we define an enumerator shape with circles, squares, rectangles, and so on
enum Shape{
//case enumeration values followed by parentheses are associated values
case circle(radius: Double)
case rectangle(width: Int, height: Int)
case square(width:CGFloat)
}
let square = Shape.square(width: 20.0)
print(square)
Copy the code
When associated values are used, there is no RawValue, mainly because a case can be represented as a set of values, and RawValue is a single value
If we want to read, we can use the switch matching mode to read your values, if we don’t want to match all cases, use the defalut keyword
switch square{
case .circle(radius: let radius):
print(radius)
case .rectangle(width: let width, height: let height):
print(width+height)
case .square(var width):
print(width)
width += 10
print(width)
}
Copy the code
If we want to change parameters, we can define them using var, but only inside the closure, not outside. Because it’s a value type
Unless we reassign
3. Enumeration size
3.1 No – content enums
As you can see, enumerations of this type are similar to enumerations in C. The default type is Int. And how much memory does it take up? Here we can measure the current enumeration directly using MemoryLayout
Print out the enumerated size and stride are all 1. The enumeration layout in Swift is always trying to use the minimum space to store enum. For the current number of cases, UInt8 can represent 256 cases, which means that if a default enumeration type has less than 256 cases with no associated value, The current enumeration types are all 1 byte in size.
From the above print, we can see intuitively that the contents of the five variables a, b, c, d and e are 00, 01, 02, 03,04 respectively, which is consistent with the layout understanding we said above.
3.2 single – content enums
How is the size calculated when the case we enumerated has a single load
Bool is 1 byte, our enumeration defaults to 1 byte so we can currently represent 256 cases. For Booleans, we only need to use low zeros and ones, and the rest of the space can be used to represent cases with no load.
- We use
Int
Int in Swift is 8 bytes, the maximum storage is 8 bytes, at which point we need to create extra space to store our case, so 8+1 = 9;
3.3 mutil – content enums
Above we are analyzing a load, when there are multiple loads
The size is still 1 byte, but we see that the storage has changed, so what does 00, 41, 80, 81, c0 mean? First, a bool requires 1 byte, or 8 bits. For bool types, we store only 0 or 1 bits. We only need 1 bits, so the remaining 7 bits are called common spare bits. For the current case, we can store them in common spare bits. So you only need 1 byte.
So if we look at 00, 41, 80, 81, c0 the first 0, 4, 8 is called the tag value the next 0, 1 and we’re going to do the tag index here
When we have multiple cases
Our analysis is the same as above: find the size of the association value and take the largest corresponding enumeration association value, so: 8+1 = 9
Enum size = maximum associated value size + case (enumerated value) size = 16 + 1 = 17, and stride due to 8-byte alignment, so automatic completion to 24
There’s only one case, we don’t need anything to differentiate the current case, so when I size you’ll notice 0.
4. To summarize
- Enumerations are usually used to represent some
basis
Style, such as network requeststate
, the user’sstate
And so on. Usually we willSpecify the type
If not, the default isThe Int type
. We canThe specified
Enumeration value, default if not specifiedrawValue
.Type String
The enumeration default is usThe case of the definition of
;Int
Type is enumeratedThe subscript index
, we specify a certain oneThe Int value of the case
Behind,case
The value of the automaticcumulative
. - Enumerations can also be used
The associated values
To better express the effect we want, such as some parameters of the shape we passswitch
To read, do not write each can be useddefalut
. - Size of enumeration
The default is 1 byte
, if the type is not specifiedInt
Type that can be stored256
One case, more than thatUint8 - > UInt16 - > UInt32 expansion, etc
. Enumeration of size =Maximum associated value in case
+Enumeration value size
. For associative valuesBool
Type, stored in0/1
So you can useThe remaining locations store cases
. - We can think of enumerations as one
A consortium
Some aspects are very similar.