import UIKit var greeting = "Hello, playground" //: [Previous](@previous) //: ### 常量 // 只能赋值一次,用let声明,它不要求在编译期间确定 // 常量以及变量在初始化之前都不能使用 let age: Int age = 31 let name = "Zhang Mengxu" let sex: String = "man" //: ### 标识符 // 标识符(比如常量名、变量名、函数名)几乎可以使用任何字符 // 标识符不能以数字开头,不能包含空白字符、制表符、箭头等特殊字符 //: ### 常见的数据类型 /*: 常见的数据类型: - 值类型(value type) - 引用类型(reference type) 值类型(value type) 值类型包含: - 枚举(enum): Optional - 结构体(struct): Bool、Int、Float、Double、Character、String、Array、Dictionary、Set 引用类型(reference type) 引用类型包含: - 类(class) */ //: #### 整数类型 /*: 整数类型:Int8、 Int16、 Int32、 Int64、 UInt8、 UInt16、 UInt32、 UInt64 在32bit平台,Int等价于Int32, Int等价于Int64 整数的最值:UInt8.max、 Int16.min 一般情况下,都是直接使用Int即可 */ let a:Int = 10 print(Int.max) //: #### 浮点类型 /*: 浮点类型:Float,32位,精度只有6位Double,64位,精度至少15位 */ let letFloat:Float = 30.0 let letDouble:Double = 30.0 //: #### 字面量 // 布尔值 let bool = true // 字符串 let string = "Zhang Mengxu" // 字符 let character: Character = "🐶" // 整数 let intDecimal = 17 // 十进制 let intBinary = 0b1001 // 二进制 let intOctal = 0o21 // 八进制 let intHexadecimal = 0x11 // 十六进制 // 浮点数 let doubleDecimal = 125.0 // 十进制,等价于1.25e2、 0.0125、 1.25e-2 let doubleHexadecimal1 = 0xFp2 //十六进制,意味着15x2^2,相当于十进制的60.0 let doubleHexadecimal2 = 0xFp-2 // 十六进制,意味着15x2^-2,相当于十进制的3.75 // 数组 let array = [1, 3, 5, 7] // 字典 let dictionary = ["name": 34, "age": 18] //: #### 类型转换 // 整数转换 let int1: UInt16 = 2_000 let int2: UInt8 = 1 let int3 = int1 + UInt16(int2) // 整数浮点数转换 let int = 3 let double = 0.14159 let pi = Double(int) + double let intpi = Int(pi) //: #### 元组(tuple) // 元组的定义 let httpError = (404, "Not Found") // 元组访问 httpError.0 httpError.1 print("The status code is \(httpError.0)") // 元组的赋值 let (statusCode, statusMessage) = httpError print("The status message is \(httpError.1)") // -代表的是不接收参数值,Not Found会忽略 let (justTheStatusCode, _) = httpError // 元组中添加参数,使用的时候通过名称访问,而非数字 let http200Status = (statusCode: 200, description: "ok") print("The status code is \(http200Status.statusCode)") //: [Next](@next)