Swift是苹果公司2014年推出的全新编程语言,继承了C、Objective-C的特性,目前已经发布了Swift 3;它是一种现代的强类型的语言,具有兼容、开源、简洁的特点,也是一种安全的语言,苹果公司为了让大家快速学习Swift语言,从Xcode6.0添加了playground交互开发,可以实时查看执行结果。和大多数语言一样,Swift也有数据类型、运算符、流程控制、函数、类、枚举、结构体、泛型,也有自己的特点,如:协议。
Swift学习官网:https://developer.apple.com/swift/
一、数据类型:
Swift包含了C和ObjC语言中的所有基础类型,Int整形(有符号:Int8 Int16 Int32 Int64 Int,无符号:UInt8 UInt16 UInt32 UInt64 UInt),Float和Double浮点型,Bool布尔型,Character字符型,String字符串类型;当然还包括enum枚举、struct结构体构造类型;Array数组、Set集合、Dictionary字典集合类型;不仅如此还增加了高阶数据类型元组(Tuple),可选类型(Optinal)。使用playground之前,先了解下Swift定义常量与变量的关键字,let是定义常量,var定义变量。
//整型的位数 var imInt: Int = 80; Int.max; Int.min; var imUInt: UInt = 80; UInt.max; UInt.min; var imInt8:Int8 = 4; Int8.max; Int8.min; var imUInt8:UInt8 = 4; UInt8.max; UInt8.min; var imInt16:Int16 = 4; Int16.max; Int16.min; var imUInt16:UInt16 = 4; UInt16.max; UInt16.min; var imInt32:Int32 = 4; Int32.max; Int32.min; var imUInt32:UInt32 = 4; UInt32.max; UInt32.min var imInt64:Int64 = 4; Int64.max; Int64.min; var imUInt64:UInt64 = 4; UInt64.max; UInt64.min //进制型数据 //默认是十进制 let decimalInt:Int = 17; //二进制 let binaryInt: Int = 0b10001; //八进制 let octalInt: Int = 0o21; //十六进制 let hexInt: Int = 0x11; let bignum = 1_000_000; //浮点型 let imFloat: Float = 3.1415926; let imDouble: Double = 3.1415926; //默认是double型 let x = 3.1415926; var aaa = 1.25e10; var bbb = 1.25e-8; var ccc = 1_000_000.000_000_1; //类型转换,只能显式强制转换 let xx:UInt16 = 100; let yy:UInt8 = 8; let m = xx + UInt16(yy); let n = UInt8(xx) + yy; let aaaa:Double = 3.0; let bbbb:Float = 0.9; let mm = aaaa + Double(bbbb); let nn = Float(aaaa) + bbbb; let integer = 3; let fraction = 0.1415926; let pi = Double(integer) + fraction; let red:CGFloat = 0.2; let green:CGFloat = 0.5; let blue:CGFloat = 0.3; UIColor(red: red,green: green,blue: blue,alpha: 1.0); //布尔类型 let imTrue: Bool = true; let imFalse: Bool = false; if imTrue{ print("I'm true"); } if !imFalse{ print("I'm false"); }@H_502_10@ 原文链接:https://www.f2er.com/swift/322837.html