Swift-8-Enumeration

Source: Internet
Author: User
Tags case statement

//Playground-noun:a Place where people can playImport UIKit//Enumeration Syntaxenumsomeenumeration {//enumeration definition goes here}//defines an enumerationenumCompassPoint { Case North Case South CaseEast CaseWest}//When you declare an enumeration, you do not assign a default value to an enumeration member like C/oc//multiple enumeration members appear on a single line separated by commasenumPlanet { CaseMercury, Venus, Earth, Mars}var directionhead=compasspoint.west//the variable directionhead is now known as the CompassPoint type, and the enumeration type can be omitted when the value is assigned againDirectionhead = . South//matching enumeration values by switchSwitchDirectionhead { Case . North:println (" North") Case . South:println (" South") Case . West:println ("West") Case . East:println ("East")default:     Break}//Association value associated valuesenumBarCode { CaseUPCA (int, int, int, int)//UPCA type (int, int, int, int)     CaseQRCode (String)//qrcode type is (String)}var ProductCode= BARCODE.UPCA (8,890, the, About) ProductCode= . QRCode ("ABCDEFG")//Extracting association values using let or var depends on usage within the case statementSwitchProductCode { Case . UPCA (let-A1, let-A2, let-A3, let A4): println ("upca:\ (A1)-\ (A2)-\ (A3)-\ (A4)")     Case . QRCode (Let S1): println ("qrcode:\ (S1)")    default:         Break}//Raw Value RAW valuesenumAsciicontrolcharacter:character { CaseTab ="\ t"     Caselinefeed ="\ n"     CaseCarriagereturn ="\ r"}//Note: The original value and the associated value are different. For an enumeration member of an attribute, its original value is always the preset value at the time the enumeration is defined, and the associated worth is set when you create a variable or constant from an enumeration member, which can be different each time//When an integer [Ntegers] is used to initialize the original value, if the other member does not specifically specify the initial value, the original value will be self-incrementenumSomeplanet:int { CaseMercury =1, Venus, Earth, Mars, Jupiter, Santurn, Uranus, Neptune}//accessing enumeration members through the RawValue propertyLet Earthsorder =SomePlanet.Earth.rawValue//initializing an instance with raw valuesLet possibleplanet = Someplanet (rawValue:7)//The return value is optional?

Swift-8-Enumeration

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.