The enumeration syntax for Swift tutorials

Source: Internet
Author: User

Import Foundation//mark:-------Enumeration Syntax-----------//Not like C and objective-c.  Swift's enumeration member will not be given a default integer value when it is created enum compasspoint{case, South case, West}enum planet{case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, nepturn}var directiontohead = Compasspoint.westdirectiontohead =. E Astswitch 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 Stores no matter what type of instance value. Assuming the need. The data type of each member can be a different enum barcode{case UPCA (int, int., int) case QRCode (String)}var Productbarcode = BARCODE.UPCA (8, 85 909_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)-----------//Raw value can be a string. character, or whatever the integer value or floating-point value. Each primitive value must be unique within its enumeration declaration. When an integer value is used for the original value, assuming that the other enumeration members do not have values, they are actively incrementing themselves.

Enum planetraw:int{Case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, neptune}//use the Toraw method of the enumeration member to 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 for queue priority, default 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 the background thread")///The first parameter is the number of times. The third part of the block blocks the shape to distinguish the number of times.

Dispatch_apply (Array.count, queue, {(index:int), Void in print (String (index) + "---" + array[int (Index) ]})//callback main thread. Run UI Update Dispatch_async (Dispatch_get_main_queue (), {(), Void in print (Nsthread.currentthread (). Ismai Nthread? "This is the main thread": "This is the background thread")})


The enumeration syntax for swift tutorials

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.