The swift version of the enumeration variable
Swift's enumeration type is very similar to the normal class, when used, do not think he is a constant, the following is the test with the source code
////Viewcontroller.swift//Swiftenum////Created by youxianming on 15/10/9.//copyright©2015 year Zipeiyi. All rights reserved.//Import UIKitenumPlanet:int { CaseMercury =1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}enumcompasspoint:string { CaseNorth , South, East, West}enumBarcode { CaseUPCA (int, int, int, int) CaseQRCode (String)}enumMinionindex:int { CaseDAVE, BOB, JERRY, JORGE, KEVIN, MARK, PHIL, STUART, TIM .StaticLet Minionnames =[DAVE:"Dave", BOB:"Bob", JERRY:"Jerry", JORGE:"Jorge", KEVIN:"Kevin", MARK:"Mark", PHIL:"Phil", STUART:"Stuart", TIM:"Tim"] Func minionname ()-String {ifLet Minionname =Minionindex.minionnames[self] {returnMinionname}Else { return "Minion"}} func minionimage ()UIImage? { returnUIImage (named:"minion\ (Minionname ())") }}classViewcontroller:uiviewcontroller {Overridefunc viewdidload () {super.viewdidload () normalexample () planetexample () Compasspointexample () barcodeexample () Minionindexexample ()} func Normalexample () {Let vegetable="Red Pepper" SwitchVegetable { Case "Celery": Print ("Add Some raisins and make ants on a log.") Case "Cucumber","Watercress": Print ("That's would make a good tea sandwich.") CaseLet XwhereX.hassuffix ("Pepper"): Print ("is it a spicy \ (x)?") default: Print ("everything tastes good in soup.")}}} func Planetexample () {ifLet planet:planet = Planet (rawValue:1) { //Switch Operation SwitchPlanet { Case . Mercury:print ("\ (planet) \ (Planet.rawvalue)") Case . Earth:print ("\ (planet) \ (Planet.rawvalue)") Case . Neptune:print ("\ (planet) \ (Planet.rawvalue)") default: Print ("\ (planet) \ (Planet.rawvalue)") } } Else { //There is no such enumeration valuePrint"No value")}}} func Compasspointexample () {ifLet Compasspoint:compasspoint = CompassPoint (rawValue:"Kxo") { //Switch Operation SwitchCompassPoint { Case . North:print ("\ (compasspoint) \ (compasspoint.rawvalue)") Case . West:print ("\ (compasspoint) \ (compasspoint.rawvalue)") default: Print ("\ (compasspoint) \ (compasspoint.rawvalue)") } } Else { //There is no such enumeration valuePrint"No value")}}} func Barcodeexample () {Let BarCode= BARCODE.UPCA ( -, -,Ten, -) SwitchBarCode { Case. UPCA ( -, -,Ten, -): Print ("YES") default: Print ("NO")}}} func Minionindexexample () {print (MinionIndex.DAVE.minionImage ())}}
Enumeration types for RawValue types
Can take parameters, can take a method
Non-rawValue type
The swift version of the enumeration variable