Swift教程之枚举语法
2015-07-10 11:30
483 查看
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 ? "这是主线程" : "这是后台线程") }) })
相关文章推荐
- swift(三)字典数组操作
- Swift教程之结构体
- swift(二)swift字符串和字符和逻辑运算
- swift(二)swift字符串和字符和逻辑运算
- swift(一)基础变量类型
- Swift教程之扩展
- Swift教程之运算符
- swift 创建第一个UIAlertView 和UIActionSheet
- swift 创建tableView 并实现协议
- 【Swift】 - 函数(Functions)总结 - 比较 与 C# 的异同
- Swift面向对象中类和对象的属性
- Swift调用sha1算法
- Swift辛格尔顿设计模式(SINGLETON)
- Swift开发教程--如何清除模拟器的数据
- Swift学习笔记系列——(5)函数
- swift 给导航增加item,实现界面的跳转
- Swift开发教程--Xcode6在iPhone模拟器上编译上下留有黑块问题
- Swift开发教程--Unused variable 'fileName'问题的解决
- 如何在Swift中使用NSError
- Swift语法之 ---- ?和!区别