Object-C ---) Swift (8) Enumeration
Declared Enumeration
Swift uses enum to define enumeration. The syntax format is
Enum enumeration name {// use the case keyword to list all enumerated values // other enumerated members}
Swift allows each enumerated value to use one case keyword, and multiple enumerated values can be defined using one case. Multiple enumerated values are separated by commas. From the perspective of program readability, the swift enumeration name must be composed of one or more meaningful words. Each word has an upper-case letter and all other letters have lower-case letters, do not use any delimiter between words.
enum Weekday{case Mondaycase Tuesdaycase Wednesdaycase Thursdaycase Fridaycase Staturdaycase Sunday}
Call Enumeration
var day:Weekday day=Weekday.Sunday
When the program can infer the enumerated type of a variable, Swift allows omitting the prefix of the enumerated name before the enumerated value.
day=.Sunday
Enumeration value and switch statement
Var chooseDay = Weekday. Sundayswitch (chooseDay) {case. Monday, Tuesday, Wednesday, Thursdayv, Friday: print ("workday") case. Staturdayv, Sundayprint ("Weekend ")}
Original Value
You can specify a simple type (such as Int, Double, Float) for each enumerated instance by using the original value)
The syntax format is as follows:
Enum enumeration name: original value type {case enumeration value = original value}
Note that Swift does not require you to specify the original value for each enumerated value. Swift can infer the enumerated values of all the enumerated values before and after the specified enumerated values based on the enumerated values.
enum Weekday:Int{case Monday,Tuesday=1,Wednesday,Thursday,Friday,Saturday,Sunday}
From Tuesday = 1, we can infer that Monday is equal to 0, Wedding is equal to 2, and later is equal to 3, 4...
However, strings cannot be inferred. You must specify the original value for each enumeration.
enum Season:Character{case Spring="S"case Summer="U"case Fall="F"case Winter="W"}
The following constructor and attributes can be used for enumeration:
Init? (RawValue :) constructor that may fail. Therefore, the * constructor returns an optional type that contains enumeration values.
RawValue: attribute (called by enumeration instances ). Obtains the original value of the specified enumerated value.
Var day = Weekday. Staturdayprint (". Saturday's original value is \ (day. rawValue)") // output 8var mySeason = Season (rawValue: "S") if mySeason! = Nil {print (mySeason !) Print ("Spring ")}
Note: It may be a failed constructor, so you need to judge it, otherwise it will cause a crash.
Associated Value
When defining an enumeration, you can add a variety of enumeration attributes for the enumeration, which greatly enriches the enumeration function compared with C and Object-C.
Enum Season {case Monday (Work: String) case Tuesday (Work: String) case Wednesday (Work: String) case Thursday (Work: String) case Friday (Work: String) case Staturday (Work: String) case Sunday (Work: String)} var mySeason1 = Season. monday (Work: "Work ah") var mySeason2 = Season. sunday (Work: "dating ")
To access the associated values of enumerated values, you must declare additional variables or constants and bind the associated values of enumerated values to these variables or constants.
Season.Monday(Work:String)=mySeason1print("\(Work)")