5.1 Swift's enumeration (enum) type and traversal [Swift original tutorial]

Original: http://coolketang.com/staticCoding/5a9925ac7565717957672a3b.html

1. This lesson will explain the use of enumeration types. Enumerations are a set of related values ​​that define a common type so that they can be called in code in a type-safe manner. 



2. You can define an enumeration with the enumeration keyword, and use curly braces to enclose the entire enumeration body. As with other types, enumeration names should start with a capital letter. 


3. Then in the enumeration body, use the keyword case to indicate that this line will define a new enumeration member. 


4. In the same way, define the other three enumeration members. 


5. If you need to get an enumeration member, just use the enumeration name with the dot syntax. 


6. Multiple enumeration members can also be defined on the same line by separating them with commas. 


7. When assigning a member of an enumeration to a variable, the type of the variable is automatically inferred as the enumeration type. 


8. Once the type of a variable is inferred or declared, it can be set to a different value of the same enumeration type simply using a dot syntax. 


9. Enumeration type members can be matched using conditional statements. First define a constant of the enumeration type and set its value to a specified member of the enumeration. 


10. Add a conditional statement to match against the variable. 


11. Then output different logs in the console according to the different values. The matching results are in the live feedback area to the right of row 24. 


12. Define a new enumeration and set the value type of the enumeration members to character. 


13. Developers can set default values ​​for enumeration members. Three enumeration members are defined here, and their values ​​are initialized, thereby setting the default value of the enumeration members. 


14. Finally, use the dot syntax to get the specified enumeration member. 


This article is organized from: "Swift4 Interactive Tutorial", the real [Hands-on] teaching mode, get started with iOS development and Swift language at the fastest speed, free download from Apple Store App Store: https://itunes.apple.com/cn/app/id1320746678 , or scan the QR code at the bottom of this page . Course supporting material download address: data download


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325383232&siteId=291194637