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,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 ? "这是主线程" : "这是后台线程") }) })
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。