Swift Learning Note (8): Enumeration

Source: Internet
Author: User

Directory:

    • Basic syntax
    • Associated value
    • Original value

Enumerations define a common type for a set of related values.

? You can specify the original value type for an enumeration member: string, character, integer value, or floating-point number, etc.
An enumeration member can specify that an association value of any type is stored in an enumeration member
Enumeration can define constructors and follow protocols
? Enumeration is a value type

Basic syntax
enum CompassPoint { Case North Case South CaseEast CaseWest}//multiple member values can appear on the same lineenum Planet { CaseMercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}var directiontohead= Compasspoint.west//defining enumeration variables and assigning valuesDirectiontohead =. East//Assigning a value to an enumeration variable of a known type//using enumerations in SwitchLet someplanet =Planet.earthswitch someplanet { Case. Earth:print ("Mostly Harmless") Default:print ("Not a safe place for humans")}

Associated value

You can define an association value of any type for an enumeration, and each associated value type can be different.

enum Barcode { CaseUPC (int, int , int, int) CaseQRCode (String)}var Productbarcode= BARCODE.UPC (8,85909,51226,3)//Assigning BarcodesProductbarcode =. QRCode ("Abcdefghijklmnop")//can also be assigned a value of two-dimensional code//extracting enumeration associated valuesSwitch Productbarcode { Case. UPC (Let-Numbersystem, let-Manufacturer, let-product, let-check): Print ("UPC: \ (numbersystem), \ (manufacturer), \ (product), \ (check).") Case. QRCode (Let ProductCode): print ("QR code: \ (productCode).")。//Print Output}//all associated values of an enumeration member are extracted as constants or variables, using let or VarSwitch Productbarcode { CaseLet . UPC (Numbersystem, manufacturer, product, check): Print ("UPC: \ (numbersystem), \ (manufacturer), \ (product), \ (check).")  CaseLet . QRCode (ProductCode): Print ("QR code: \ (productCode).") }

Original value

The original value is different from the associated value, and the original value is a fixed, immutable value that is prepopulated when the enumeration is defined, and the associated value is a mutable value that is set only when the enumeration constant or variable is created.

//the original value of the first element is assigned a value of 1, and then the element value incrementsenum Planet:int { CaseMercury =1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}//Compasspoint.south has an implicit primitive value Southenum Compasspoint:string { CaseNorth , south, east, West}//use the enumeration member RawValue property to access its original valueLet Earthsorder =Planet.earth.rawValue//to create an optional enumeration constant using the enumeration primitive valuesLet possibleplanet = Planet (rawValue:7)//possibleplanet type is Planet? value is Planet.uranus

Statement: This series of content is from the network or electronic books, only to do learning summary!

Swift Learning Note (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.