声明常量与变量,分号可加可不加
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0
var x = 0.0, y = 0.0, z = 0.0
类型注释
var welcomeMessage: String
welcomeMessage = "Hello"
var red, green, blue: Double
打印
可以直接打印当前常量或变量:
print(friendlyWelcome)
//"Bonjour"
拼接打印:
print("The current value of friendlyWelcome is \(friendlyWelcome)")
// "The current value of friendlyWelcome is Bonjour!"
注解
单行:
// This is a comment.
多行:
/* This is also a comment
but is written over multiple lines. */
多行:可以嵌套使用
/* This is the start of the first multiline comment.
/* This is the second, nested multiline comment. */
This is the end of the first multiline comment. */
整数
swift提供提供的整数有8、16、32、64位,例如Int8、Int32...
整数的范围:
let minValue = UInt8.min // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max // maxValue is equal to 255, and is of type UInt8
Int 与 UInt
在32位平台上,Int就等于Int32;
在64位平台上,Int就等于Int64;
UInt同理;
小数
Double 代表64位浮点数;
Float 代表32位浮点数;
类型推断
整数默认推断为Int:
let meaningOfLife = 42
小数默认推断为Double:
let pi = 3.14159
let anotherPi = 3 + 0.14159
字面量
let decimalInteger = 17
let binaryInteger = 0b10001 // 17 二进制
let octalInteger = 0o21 // 17 八进制
let hexadecimalInteger = 0x11 // 17 16进制
字面量可以加额外的零和下划线,便于读取:
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1
指数
十进制:
1.25e2 = 1.25 x 10^2= 125.0.
1.25e-2 means 1.25 x 10^-2, or 0.0125.
十六进制:
0xFp2 =15 x 2^2= 60.0.
0xFp-2 = 15 x 2^-2= 3.75.
类型转换
小范围往大范围转:
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)
let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
小数转Int回直接舍去小数点后表面数值,4.75->4,-3.9->-3:
let integerPi = Int(pi)
// integerPi=3
类型别名
关键字:typealias
相当于给人起了个外号,用法没变:
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.min
布尔值
只有true和false两个关键字
声明:
let orangesAreOrange = true
let turnipsAreDelicious = false
使用:
if turnipsAreDelicious {
print("Mmm, tasty turnips!")
} else {
print("Eww, turnips are horrible.")
}
// 打印 "Eww, turnips are horrible."
1和0不能用作布尔值判断:
let i = 1
if i {
// 编译不通过,报错
}
let i = 1
if i == 1 {
// 正确
}
元组tuples
元祖可以存放多种类型的值:
let http404Error = (404, "Not Found")
取值:
1、
let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
// "The status code is 404"
print("The status message is \(statusMessage)")
// "The status message is Not Found"
2、如果只想取部分值,其他部分可用下划线忽略掉:
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// "The status code is 404"
3、也可以直接通过索引号来取值:
print("The status code is \(http404Error.0)")
//"The status code is 404"
print("The status message is \(http404Error.1)")
//"The status message is Not Found"
Options
?:代表有值和没有值两种情况
!:有值
异常处理
使用throws抛出异常,放在方法名后面
func canThrowAnError() throws {
}
使用do-catch捕获异常
do {
try canThrowAnError()
//没有异常
} catch {
// 异常处理
}
断言与预处理
断言:assert,只有在debug模式下起作用
预处理:precondition,debug和release都起作用
程序运行到这里会崩溃,并进行提示
let age = -3
assert(age>0, "年龄不能小于0")
或者precondition(age>0, "年龄不能小于0")
网友评论