The swift version of the enumeration variable

Source: Internet
Author: User

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

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.