The enumeration syntax for Swift tutorials

Source: Internet
Author: User

Import Foundation//mark:-------Enumeration Syntax-----------//Unlike C and Objective-c, Swift's enumeration members are not assigned a default integer value when they are created enum compasspoint{ Case East Case West}enum planet{case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uran US, Nepturn}var directiontohead = Compasspoint.westdirectiontohead =. Eastswitch directiontohead{case. North:print ("North") case. South:print ("South") case. East:print ("Orient") case. West:print ("West") Default:print ("Unknown Direction")}//mark:-------instance value (associated values)-----------//You can define the enumeration of Swift Store any type of instance value, if required, each member's data type can be different enum barcode{case UPCA (int, int., int) case QRCode (String)}var Productbarcode = B Arcode. UPCA (8, 85909_51226, 3) Productbarcode =. QRCode ("Abcdefghijklmnop") switch productbarcode{case let.    UPCA (Numbersystem, identifier, check): Print ("upc-a with value of \ (Numbersystem), \ (identifier), \ (check).") Case Let. QRCode (ProductCode): Print ("QR code with value of \ (proDuctcode). ")} Output "QR code with value of Abcdefghijklmnop.//mark:-------Original value (raw values)-----------//The original value can be a string, a character, or any integral or floating-point value. Each original value must be unique within its enumeration declaration. When an integer value is used for the original value, the other enumeration members are automatically incremented if they do not have a value. Enum planetraw:int{Case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, neptune}//the Toraw method that uses the enumeration member can access the original Starting value: Let Earthsorder = PlanetRaw.Earth.rawValueprint (earthsorder)//Earthsorder is 3//mark:----------- GCD demo----------var array = ["Jack", "Rose", "Jay", "Grace"];//declares a global concurrency queue, type dispatch_queue_t;dispatch_queue_priority _default is queue priority, default is 0var queue:dispatch_queue_t = dispatch_get_global_queue (dispatch_queue_priority_default, 0)// Opens a thread Dispatch_async (queue, {(), Void in print (Nsthread.currentthread (). Ismainthread?    "This is the main thread": "This is a background thread")//The first parameter is the number of times; Dispatch_apply (Array.count, queue, {(index:int), Void in print (String (index) + "---" + array[int (Index) ]})//callback main thread, perform UI update Dispatch_async (Dispatch_get_main_queue (), {() VoID in print (Nsthread.currentthread (). Ismainthread? "This is the main thread": "This is the background thread")})

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The enumeration syntax for Swift tutorials

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.