//賦值運算符(a = b)用b的值初始化或更新a的值:
let b = 10
var a = 5
a = b
// a is now equal to 10
//如果賦值的右邊是一個具有多個值的元組,那麼它的元素可以同時分解爲多個常量或變量:
let (x,y) = (1,2)
// x is equal to 1, and y is equal to 2
//與C和Objective-C中的賦值操作符不同,Swift中的賦值操作符本身並不返回值。下列表達式無效:
if x = y {
// This is not valid, because x = y does not return a value.
}
//該特性可以防止在實際使用equal to操作符(==)時意外使用賦值操作符(=)。通過使if x = y無效,Swift可以幫助您避免代碼中的此類錯誤。
//一元減操作 數值的符號可以使用前綴-來切換
let three = 3
let minusThree = -three // minusThree equals -3
let plusThree = -minusThree // plusThree equals 3, or "minus minus three"
//一元加操作符 簡單地返回它操作的值,沒有任何變化。
let minusSix = -6
let alsoMinusSix = +minusSix
//雖然一元加號運算符實際上什麼都不做,但是當你對負數也使用一元減號運算符時,你可以用它在你的代碼中爲正數提供對稱性。
複合賦值操作
var c = 1
c += 2
// a is now equal to 3
//表達式a += 2是a = a + 2的簡寫。實際上,加法和賦值被合併到一個操作符中,該操作符同時執行兩個任務。
比較操作
/*
Equal to (a == b)
Not equal to (a != b)
Greater than (a > b)
Less than (a < b)
Greater than or equal to (a >= b)
Less than or equal to (a <= b)
Swift also provides two identity operators (=== and !==), which you use to test whether two object references both refer to the same object instance.
*/
1 == 1 // true because 1 is equal to 1
2 != 1 // true because 2 is not equal to 1
2 > 1 // true because 2 is greater than 1
1 < 2 // true because 1 is less than 2
1 >= 1 // true because 1 is greater than or equal to 1
2 <= 1 // false because 2 is not less than or equal to 1
//Comparison operators are often used in conditional statements, such as the if statement:
let name = "world"
if name == "world" {
print("hello, world")
} else {
print("I'm sorry \(name), but I don't recognize you")
}
(1, "zebra") < (2, "apple") // true because 1 is less than 2; "zebra" and "apple" are not compared
(3, "apple") < (3, "bird") // true because 3 is equal to 3, and "apple" is less than "bird"
(4, "dog") == (4, "dog") // true because 4 is equal to 4, and "dog" is equal to "dog"
三元條件操作
let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)
等同於:
let contentHeight = 40
let hasHeader = true
let rowHeight: Int
if hasHeader {
rowHeight = contentHeight + 50
} else {
rowHeight = contentHeight + 20
}
Nil Coalescing運算
(a ?? b)
1. a必須是Optional類型的。
2. b的類型必須要和a解包後的值類型一致
let defaultColorName = "red"
var userDefinedColorName: String? // defaults to nil
var colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName is nil, so colorNameToUse is set to the default of "red"
userDefinedColorName = "green"
colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName is not nil, so colorNameToUse is set to "green"
範圍操作
//閉合範圍操作 (a...b)
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
//半開範圍操作 (a..<b)
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
//單邊操作
for name in names[2...] {
print(name)
}
// Brian
// Jack
for name in names[...2] {
print(name)
}
// Anna
// Alex
// Brian
//半開單邊操作
for name in names[..<2] {
print(name)
}
// Anna
// Alex
let range = ...5
range.contains(7) // false
range.contains(4) // true
range.contains(-1) // true
邏輯操作
Logical NOT (!a)
Logical AND (a && b)
Logical OR (a || b)
//非操作
let allowedEntry = false
if !allowedEntry {
print("ACCESS DENIED")
}
// Prints "ACCESS DENIED"
//與操作 只有當兩個表達式爲真時,整個表達式才爲真。如果其中一個表達式爲false,那麼整個表達式也爲false。如果第一個表達式爲false,第二個表達式將不會被求值
let enteredDoorCode = true
let passedRetinaScan = false
if enteredDoorCode && passedRetinaScan {
print("Welcome!")
} else {
print("ACCESS DENIED")
}
// Prints "ACCESS DENIED"
//或操作 表達式有一個爲真時,整個表達式爲真。如果第一個表達式爲真,將不會對第二個表達式進行求值,因爲整個表達式爲真
let hasDoorKey = false
let knowsOverridePassword = true
if hasDoorKey || knowsOverridePassword {
print("Welcome!")
} else {
print("ACCESS DENIED")
}
// Prints "Welcome!"
//組合邏輯操作
if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
print("Welcome!")
} else {
print("ACCESS DENIED")
}
// Prints "Welcome!"
//可以添加圓括號,使表達式更加清晰
if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
print("Welcome!")
} else {
print("ACCESS DENIED")
}
// Prints "Welcome!