Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Swift- fine enumeration (enum)
Swift- fine enumeration (enum)
Declare an enumeration in one direction, which contains four enumeration values: East/West/South/North.

Swift enumeration members are complete values when they are created, and the types of these values are clearly defined direction types. They are not given default integer values like objective-C. In the direction example above, east, west, north and south are not implicitly assigned to 0, 1, 2 and 3.

Enumeration in Swift is more flexible, and there is no need to provide a value for each enumeration member. If a value (called original value) is provided to an enumeration member, the type of the value can be: string, character, arbitrary integer value or floating-point type.

Swift's enumeration type provides an implementation called RawValues, which provides a default value for enumeration items, which is determined at compile time.

The original value obtained by enumerating items is optional, so you need to use if let to make a judgment.

Enumeration value type is not specified.

Implicit original value

Enumeration items under implicit original values use system assignment:

Explicit original value

Implicit+explicit original value

The property observer is used here, and the enumeration in Swift is more like an object, so it is convenient to monitor the enumeration value by using the property observer.

In Swift, you can also define such an enumeration type, and each enumeration item has an additional information to extend the information representation of this enumeration item, which is also called associated value.

It is important to learn how to use associated values. The specific use will be described in detail in Section 9 below.

Generally speaking, enumeration is easy to judge equality. Once the associated values are added to the enumeration, Swift cannot compare them correctly, so it needs to implement the = = operator for the enumeration itself.

With the help of comparable protocols, the comparison of enumeration is realized.

You can traverse the Swift enumeration conforming to the CaseIterable protocol, and get all enumeration members through allCases.

If the interface addresses in the app are all put together, which is inconvenient to name or find, it can be designed by nested enumeration. Can be dispersed in multiple files, convenient for maintenance and management.

It is a good choice to use enumeration to manage constants in the system.

Storage attribute cannot be used in enumeration, but calculation attribute can be used, and the content of calculation attribute is obtained from enumeration value or enumeration associated value.

Here, you can think of enumeration as a class, which is introduced as a member method, with AppleDeivce.iPhone as an instance of AppleIvece and case as its property. Switch self in the introduction is actually to traverse all the scenes of this anonymous attribute, such as iPad and iPhone, and then return different values according to different scenes.

You can make a custom enumeration constructor.

Printing protocol of the system

Make enumerations conform to this protocol.

Enumeration can be extended. Case in enumeration can be separated from method/protocol, and readers can quickly digest the contents of enumeration.

Design an error information processing function under the network class.

UserDefaults are usually used in projects to store simple user information. But the maintenance of the key is not very convenient. I won't remember either. Enumeration+structure can solve this problem well.

Is it more hierarchical and convenient to design the storage module of APP in this way?

Recursive enumeration is an enumeration that takes another enumeration as the associated value of an enumeration member. When the compiler runs recursive enumeration, an indirect addressing layer must be inserted. You can use the indirect keyword before declaring an enumeration member to indicate that it is recursive. You can also declare that all enumeration members are recursive before the entire enumeration.