import Foundation //MARK:-------枚举语法----------- //不像 C 和 Objective-C 一样。Swift 的枚举成员在被创建时不会被赋予一个默认的整数值 enum CompassPoint { case North case South case East case West } enum Planet { case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Nepturn } var directionToHead = CompassPoint.West directionToHead = .East switch directionToHead { case .North: print("北方") case .South: print("南方") case .East: print("东方") case .West: print("西方") default: print("未知方向") } //MARK:-------实例值(Associated Values)----------- //你能够定义 Swift 的枚举存储不论什么类型的实例值。假设须要的话。每一个成员的数据类型能够是各不同样的 enum Barcode { case UPCA(Int, Int, Int) case QRCode(String) } var productBarcode = Barcode.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).") } // 输出 "QR code with value of ABCDEFGHIJKLMNOP. //MARK:-------原始值(Raw Values)----------- //原始值能够是字符串。字符,或者不论什么整型值或浮点型值。每一个原始值在它的枚举声明中必须是唯一的。当整型值被用于原始值,假设其它枚举成员没有值时,它们会自己主动递增。enum PlanetRaw: Int { case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune } //使用枚举成员的toRaw方法能够訪问该枚举成员的原始值: let earthsOrder = PlanetRaw.Earth.rawValue print(earthsOrder) // earthsOrder is 3 //MARK:-----------GCD演示---------- var array = ["jack", "rose", "jay", "grace"]; //声明一个全局并发队列,类型是 dispatch_queue_t;DISPATCH_QUEUE_PRIORITY_DEFAULT为队列优先级,默觉得0 var queue: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //开启一个线程 dispatch_async(queue, { () -> Void in print(NSThread.currentThread().isMainThread ? "这是主线程" : "这是后台线程") //第一个參数为次数。第三个參数 block块里面的形參是区分第几次。
dispatch_apply(array.count, queue, { (index:Int) -> Void in print(String(index) + " --- " + array[Int(index)]) }) //回调主线程。运行UI更新 dispatch_async(dispatch_get_main_queue(), { () -> Void in print(NSThread.currentThread().isMainThread ? "这是主线程" : "这是后台线程") }) })