Enumeration in Swift
Study notes from the Geek Academy
1 Import Foundation2 3 /**********1*swift defines the syntax format for enumerations *************/4 /*5 enum enum Name {6 list all enumerated values using the CASE keyword7 other members of the enumeration8 }9 */Ten //Defining Enumerations One enumseason{ A CaseSpring - CaseSummer - CaseFall the CaseWinter - } - //use a case to enumerate all values - enumseason2{ + CaseSpring,summer,fall,winter - } + //declaring variables with enumerations Avar Weather:season//here is a variable that declares a season type weather atWeather =. Summer//or: weather = Season.summer -println"1:\ (Weather)") - -var enum_1 = Season.winter//using type inference directly can also - Switch(enum_1) { - Case . Spring: in println (season.spring) - Case . Summer: to println (Season.summer) + default: -println"at Home") the}//the case in switch does not overwrite all the values of the enumeration, and you must add the default statement, which means that if all the values of the enumeration are listed, you can delete the default statement. * $ Panax Notoginseng /**********2* Original Value *************/ - /* the enum enum Value: Primitive value type { + Case Enumeration value = Original Value A } the */ + enumweekday:int{ - CaseMon,tur,wen =4, Thur,fri,sat,sun $}//because it's all plastic, and it's automatically inferred that they were 1~7 . $println (Weekday.wen)//I do not know what the result is: (Enum Value) - - //so get the original value of the enumeration the println (Weekday.Mon.rawValue) - println (Weekday.Tur.hashValue)Wuyi the enumseason3:character{ - CaseSpring ="Spring" Wu CaseSummer ="Xia" - CaseFall ="Autumn" About CaseWinter ="Winter" $}//but the inference of the above type is confined to the integer type, and swift automatically infers - - //based on the original value to get the enumeration value, call the thing constructor, this constructor is optional type: init? (RawValue:) -var Myseason = Season3 (rawValue:"Autumn") A ifMyseason! =nil{ + Switch(myseason!) {//need to solve the package indirectly the Case . Spring: -println"Spring is not a book Day") $ Case . Summer: theprintln"It's just hot in summer.") the Case. Fall,. Winter://this means "or" . theprintln"Autumn flies the winter cold") the default: -println"reading has to wait for next year") in } the } the About the /**********3* Association value *************/ the //After each enumeration value, add the associated value with the parentheses expanded, then the number of associated values is 0~ multiple, then define the type value, or, of course, only the type, do not need the associated value name the enumPlanet { + CaseEarth (weight:double,name:string) - CaseMars (density:double,name:string,weight:double) the CaseVenus (double,string)Bayi CaseSaturn the CaseNeptune the}//in fact, the parentheses in the back are the ancestors. - //get the enumeration value, the system will also automatically prompt, this operation will be able to understand -var P1 = Planet.earth (weight: at, Name:"hah") the //The next step is to get these associated values, to declare the extra variables to get the associated values . the //But the switch statement is used directly below the Switch(p1) { the CasePlanet.earth (var weight,var name): -println"\ (name) ~~~~~~~~~\ (weight)") the CaseLet planet.mars (Density:d,name:n,weight:dou): theprintln"\ (d)") the default:94 Break the}
The enumeration in swift Object-oriented foundation (top)--swift