Swift 语言之基本运算符学习笔记

赋值运算符

1
2
3
4
let b = 10
var a = 5
a = b
// a is now equal to 10

算术运算符

1
2
3
4
5
1 + 2       // equals 3
5 - 3 // equals 2
2 * 3 // equals 6
10.0 / 2.5 // equals 4.0
"hello, " + "world" // equals "hello, world"

求余运算符

1
2
9 % 4    // equals 1
-9 % 4 // equals -1

浮点数求余运算符

1
8 % 2.5   // equals 0.5

自增和自减运算符

1
2
3
4
5
var a = 0
let b = ++a
// a and b are now both equal to 1
let c = a++
// a is now equal to 2, but c has been set to the pre-increment value of 1

单目运算符

单目负号运算符:

1
2
3
let three = 3
let minusThree = -three // minusThree equals -3
let plusThree = -minusThree // plusThree equals 3, or "minus minus three"

单目正号运算符:

1
2
let minusSix = -6
let alsoMinusSix = +minusSix // alsoMinusSix equals -6

复合赋值运算符

1
2
3
var a = 1
a += 2
// a is now equal to 3

比较运算符

1
2
3
4
5
6
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

三目条件运算符

1
2
3
4
let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)
// rowHeight is equal to 90

nil合并运算符

1
2
3
4
5
6
7
8
9
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"

区间运算符

闭区间运算符:

1
2
3
4
5
6
7
8
for index in 1...5 {
println("\(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

半闭区间运算符:

1
2
3
4
5
6
7
8
9
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
println("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

逻辑运算符

逻辑非运算符:

1
2
3
4
5
let allowedEntry = false
if !allowedEntry {
println("ACCESS DENIED")
}
// prints "ACCESS DENIED"

逻辑与运算符:

1
2
3
4
5
6
7
8
let enteredDoorCode = true
let passedRetinaScan = false
if enteredDoorCode && passedRetinaScan {
println("Welcome!")
} else {
println("ACCESS DENIED")
}
// prints "ACCESS DENIED"

逻辑或运算符:

1
2
3
4
5
6
7
8
let hasDoorKey = false
let knowsOverridePassword = true
if hasDoorKey || knowsOverridePassword {
println("Welcome!")
} else {
println("ACCESS DENIED")
}
// prints "Welcome!"

组合逻辑运算符:

1
2
3
4
5
6
if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
println("Welcome!")
} else {
println("ACCESS DENIED")
}
// prints "Welcome!"

显式圆括号:

1
2
3
4
5
6
if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
println("Welcome!")
} else {
println("ACCESS DENIED")
}
// prints "Welcome!"