print(_:separator:terminator:)
参数分为三个部分
- _: items,打印的内容可以由多个组成,eg:
print("12", "13")
- separator : 分割符,显示多个 item 之间的符号,默认是空格
print(1.0, 2.0, 3.0, 4.0, 5.0, separator: " ... ")
// Prints "1.0 ... 2.0 ... 3.0 ... 4.0 ... 5.0"
- terminator:终止符,字符串之后的符号,默认是换行
字符串中含有变量
print (“I have \(appleNumber) apples”)
注释的三种方式(不进行编译)
- // This is a comment
- /* */
- /* /* */ */ 嵌套(nest)
不需要写分号(semicolons)
语句后面可以不加,但是如果多个语句写在同一行时,需要用分号隔开
数据推断
不一定要准确指定类型,编译器会自动推断出赋值的类型;推断出的类型,就是后面指定的类型
数据安全
常量设置后不可再改变,不同数据类型之间传递时不匹配会报错
数据类型
整数
- 有符号和无符号(8/16/32/64位)
- 通过 .min 和 .max 获取最大最小值
- 使用 Int(首选)和 UInt ,会根据平台自动设置大小
除非需要分配一个与平台大小一致的无符号类型使用 UInt,否则使用 Int 更好。
浮点型
- Double(首选)- 64 - 至少15位小数点
- Float - 32 - 只有6位
数字字面量
- 十进制 :无
- 二进制 :0b(字首)
- 八进制 :0o(字首)
- 十六进制 :0x(字首)
指数
- 1.25e2 = 1.25*10^2 十进制用 e or E
- 0xFp2 = 15*2^2 十六进制用 p or P
填充额外的 0 和 下划线增加可读性
let paddedDouble = 000123.456
let containUnderscore = 1_000_000.000_000_1
变量与常量
- 常量只能赋值一次 let constant = 32
- 变量 var variable: Double = 32.0(指定数据类型)
多个变量和常量的声明可以写在同一行,用逗号隔开,并且声明同一种类型
var x = 0.0, y = 0.0, z = 0.0: Double
如果有初始值,一般不需要写后面的类型标记
var variable = 32.0
命名字符:能够包括几乎所有字符(包括 emoji,中文)
命名规则:不能包括空格、数字符号、箭头、无效 Unicode 编码、连线或者制表符、不能以数字开头
如果需要用 Swift 的关键字来命名需要在关键词两边加上"`"(不建议使用)
一旦声明了变量或者常量的类型,不能以相同名字再次声明,或者改变不同类型的存储值,也不能在变量和常量之间进行切换
值不会进行隐式转换,转换时需要指定类型
let label = “The width is"
let width = 4
let widthLabel = label + String(width)
数据类型转换
每种数据类型都有存储的数字范围,超过范围会报错。
let twoThousand:UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)
// UInt16(one) 会重新实例化一个 UInt16 对象
如果值没有显示类型,会推断出该值的数据类型
let anotherPi = 3 + 0.14159 // 推断出 3 为 Double
数字文字的相加和数字变量或数字常量的相加规则不同
let three = 3 // int
let pointOneFourOneFiveNine = 0.14159 // Double
let pi = Double(three) + pointOneFourOneFiveNine
浮点型转整型,直接截断后面的小数点 eg: 3.75 -> 3
类型别名
定义一个可以替换已知类型的别名
typealias AudioSample = UInt16
布尔类型
bool 的常量 ture 和 false; if 中的条件不能用非 bool 类型代替
let one = 1
if one {
// error
}
元组
复合值:可以包含多种数据类型的数据
使用方法:非常使用于方法返回多值使用
let http404Error = (404, "Not Found")
let http200Error = (statusCode: 200, description: "OK");
let (justTheStatusCode, _) = http404Error // "_" 忽略
print("The status message is \(justTheStatusCode)")
// http404Error.0 = http200Error.statusCode = 404
// http404Error.1 = http200Error.message = "Not Found"
可选值
- optional 可能存在两种情况(能够使用的值和 nil )
- OC 中的 nil 只能作用于对象(指针,指向空对象),而 Swift 的 nil 可选值可适用于任何类型
- 定义一个可选值没有默认值,自动设置 nil,nil 不能被非可选类型的值使用
let possibleNumber = "123"
let covertedNumber = Int(possibleNumber) // 这里转换的是 optional int
if 语句:(适用于强制/隐式解包)检查可选值是否为空
if covertedNumber != nil {}
可选绑定:(适用于强制/隐式解包)使用 if / while / guard 实现
if let constantName = Int(possibleNumber) { // constantName 可用 let or var; 可以包含多个可选捆绑,用逗号隔开
// optional 有值,constantName = optional 不需要 !解包访问
}else {
// optional nil
}
强制解包
使用可选值时确认有值,可以在名字后面加 “!”,定义时,在类型后面加"?"。若运行时该值为 nil 将会报错。
let optional: String? = "String"
let forcedString: String = optional!
隐式解包
确认可选值在以后使用时都有值,定义时,在类型后面加"!",之后使用可选值时自动解包。若运行时该值为 nil 将会报错。
let assumedString: String! = "An implicitly unwrapped optional string"
let implicitString: String = assumedString
错误处理
- throws : 标记一个方法能够抛出错误。
- throw :在方法中抛出一个错误,如果在方法中抛出错误,方法将会马上 return 并处理错误。
func canThrowAnError() throws {
// throw PrinterError.
}
处理错误的方式:
do {
// 方法实现前加 try 抛出错误
try canThrowAnError()
// eatASandwich
} catch {
// 接收错误
// 默认给出名为 error 的错误
print(error)
} catch {
// 支持多 catch
}
断言 Assert 和 先决条件 Precondition
- 进行条件判断,若为 YES 继续执行,NO 程序结束
- Asset 用于 Debug,Precondition 用于生产,帮助找出错误。
- 相比于错误处理,这两种处理方式并不能捕获错误断言
let age = -3
assert(age >= 0, "A person's age can't be less than zero")
// 如果不需要条件判断
assertionFailure("A person's age can't be less than zero")
如果编译器是非检测模式(-Ounchecked),preconditions 不会检查。编译器将假设所有条件都是 ture。fatalError(_:file:line:) 可以会忽略优化选项,这个总是会终止,被创建在未实现的方法中。
网友评论