//: Playground-noun:a Place where people can playImport UIKit//------Enumeration---------////enumerations in 1.Swift//describe a person's gender (0: Male, 1: female, 2: unknown)//enum Sex {//Enum Type//Case Male//enumeration Values//Case Female//Case Unknown////}//////Define Enumeration variables//var mysex:sex = Sex.female//mysex =. Male////var yoursex:sex =. Unknown////if yoursex = =. Unknown {// //print ("This person is like a man and a woman")//}//////2. Enumeration and switch use in conjunction///*//to ensure security, switch and enum types are used in combination://(1) List all enumerated values, so the situation should be considered//(2) or use default to indicate defaults//*///Switch Mysex {//Case Sex.male://print ("Boys")//Case Sex.female://print ("Schoolgirl")////case Sex.unknown:////print ("Unknown")//Default://print ("unrecognized")// //}//3. Original value of the enumeration//in Swift, the enumeration value is not the default integer, and is different in C,oc. enumSex:int { CaseMale =0 CaseFemale =1 CaseUnknown =2 }//print the original value of an enumeration valueprint (Sex.Unknown.rawValue)//The types of enum primitive values are extended in Swift: String, Character, Int, Float, Double//enum Newsex:string {//Case Male = "Male"//Case Female = "Female"//Case Unknown = "Unknown"// //}//implicit assignment of the original value://String: Default and enumeration values have the same name//Int: Default starting from 0, increment by 1enumnewsex:string{ CaseMale, Female, Unknown}print (NewSex.Female.rawValue)//Initialize variables with primitive valuesvar hissex =Sex.male//enumerates the constructors-returns is an optional type and returns nil if the incoming raw value is an illegal valuevar hersex = Sex (rawValue:5)//Optional BindingsifLet Yoursex = Sex (rawValue:Ten) { SwitchYoursex { CaseSex.Male:print ("Boys") CaseSex.Female:print ("Schoolgirl") CaseSex.Unknown:print ("Unknown") }} Else{print ("without this sex") }//4. Enumerated associative values (related values): Store enumeration values some extra information, related values can be different types of data//(1) Basic grammarenumColor { CaseRed (alpha:float) CaseBlue (Alpha:float, Y:float, l:int)}var Mycolor:color= Color.Red (alpha:0.5) MyColor= . Blue (Alpha:1Y: -L -)//if MyColor = = Color.Red (alpha:0.3) {// // //}SwitchMyColor { Casecolor.red (let Alpha): print ("Red alpha = \ (Alpha)") CaseColor.Blue (Let-A, let-B, let C): print ("Blue a=\ (a), b=\ (b), c=\ (c)") }enumBarCode { CaseUp }
Swift enumeration _003_swift enum type