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