Enumeration is introduced

  • Implicit allocation mechanism

It starts at 0, starts at FRI and is specified to start at 10, and looks at RawValue of StringCopy the code
  • RawValue

View SIL fileFrom the figure above, it is calledgetterMethods toGet enumeration valuesLook at the getter methodReturns the constructed stringThe build string is inThis is built at compile timeAnd stored inMachoOf the file__TEXT.cstringThe followingAnd the way he builds it is by going toFind the corresponding address under __text. cstring, take out the corresponding string

  • The difference between Raw and enumerated values

In the figure aboveWeak.mon. rawValue is the same as weak.mon, but itDoesn't mean they're the sameAs can be seen from the following assignmentWeak.MON cannot be assignedtherawValueisType Stringthe

whileMONisWeak Type enum Value

  • Enum initialization

How does enum call init

Init MON prints an optional value, but init PH returns nil

View sil file

initIs aOptional initializationMethod, check initI’m going to create oneAn array ofAnd then returnA tupleIn the yuan zuContains an array(the first element of the meta-ancestor), and one morePointer to the array in the progenitorThrough thePointer to thetheThe offsetthemachoIn theEnumeration values are placed consecutively into an array of primitives One was called after the enumeration value was put down_findStringSwitchCaseMethod, in swift source code to findReturn to the SIL fileIf you don’t have the same

  • The associated values

Often used to indicate complex definitions

  • Pattern matching

Is to match all enumerated values

  • Correlation value pattern matches

It can also be matched by an if, which means the same as the switch aboveYou can match to get a meta-ancestor association value in another enumerated valueDifferent matching values can change the value of x

  • Enumeration nested

Allow nested combinations of forms!

Nested enumerations within the structureThe enumeration ofContains propertiesThe enumeration ofOnly computed and type attributes can be included.Storage attributes cannot be includedBecause theYou can have storage properties in a structure and the size of the structure contains the size of the storage propertyAnd theThe size of the enumeration is related to the number of cases

Enumeration include method

Enumeration size

  • If there is a rawValue in the enumeration

Enumeration in memory is toOne byte of storageIn memory, caseThe default is UInt8That is1 byte.No matter how many cases there are.1 byte stores a maximum of 256 bytes, if it exceeds 256,UInt8 will automatically upgrade to UInt16How does it go up even furtherYou can see that it’s continuously stored in memory

  • Enumeration containing associated values

containsThe size of an enumeration with associated values depends on the type of the associated value, the associated values are also requiredDepends on the association value of the largest caseAbove, the rectangle has a larger association value than the Circle

In the figure above, the memory occupied by case A will change if the associated value type is changed

Real storage location: the type is the size of the memory of that store very added: when memory is insufficient to byte alignment of supplement insufficient named the place Invalid: when an enum of memory the biggest case of adding a supplement with other case a have overlap each other, these overlap each other, known as invalidCopy the code
The associated value type of case A in enum tempA is Int, which takes 8 bytes and does not require byte alignment. The memory used by case B's associated value types is 1,8,2, and 1+7,8,2. Since we need to store a case type, check to see if there are any invalid bits in other cases that coincide with the supplementary bits in case B. If there are no invalid bits in other cases, add one more bitCopy the code

In enum tempB, the associated value type of Case A is Int8, which occupies 1 byte and requires byte alignment. The memory used by case B's associated value types is 1,8,2, and 1+7,8,2. If a case type is to be stored, check whether there are invalid bits in other cases that overlap the complementary bits in case B and case A. The compiler stores the case type in the last bit of the case supplement bit to save memory for optimization, so it takes up 18 bitsCopy the code