簡單的基本操作不在這一一講解,可以查看相關的鏈接進行學習
1.元組的比較
按照從左到右、逐值比較的方式,若大小比較,其中一個元素成立,則返回true
相等比較時,需要滿足所有的元素均相等
(1, "zebra") < (2, "apple") // true,因為 1 小于 2
(3, "apple") < (3, "bird") // true,因為 3 等于 3,但是 apple 小于 bird
(4, "dog") == (4, "dog") // true,因為 4 等于 4,dog 等于 dog
2.空合元算符
a != nil ? a! : b //當a不為空的時候,a!強制解析;當a為nil的時候,b賦值,b不能為nil
等價于
let defaultColorName = "red"
var userDefinedColorName: String? //默認值為 nil
var colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName 的值為空,所以 colorNameToUse 的值為 "red"
3.區間運算符
a...b 表示區間
- 1...5(包括1和5)
- 1 ...<5(包括1,不包括5)
- 1<...5(包括5,不包括1)
for index in 1...5 {
print("\(index) * 5 = \(index * 5)")
}
// 1 * 5 = 5
// 2 * 5 = 10
// 3 * 5 = 15
// 4 * 5 = 20
// 5 * 5 = 25
參考文獻:
極客學院 - 基本運算符