Swift enumeration _003_swift enum type

Source: Internet
Author: User

//: 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.