基本运算符
1.Terminology 术语
运算符有一目,双目和三目运算符
一目运算符对一个对象进行操作,如-a,区分前置符和后置符, 如!b , i++
双目运算符用于两个对象之间的操作,如 2+3
三目运算符操作与三个对象之间,Swift只有一个三目运算符 a?b:c
2.Assignment Operator 赋值运算符
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
// Swift的赋值操作不返回任何值,if x=y 是错误代码,让你不会再把 == 错写为 = , Swift帮你避免了这种代码错误
if x = y {
// this is not valid, because x = y does not return a value
}
3.Arithmetic Operators 数值运算
Swift不允许数值运算中出现溢出,不过可以使用溢出运算符,比如(a &+ b)
1 + 2 // equals 3
5 - 3 // equals 2
2 * 3 // equals 6
10.0 / 2.5 // equals 4.0
//加法也用于字符串的拼接,两个Character 或一个String一个Character的拼接
"hello, " + "world" // equals "hello, world"
4.Remainder Operator 求余
求余运算在其他语言也有,其实是一样的,原文讲的很详细,在对负数b求余,b的符号被忽略,即 a%b 和 a%-b 的结果是一样的
9 % 4 // equals 1
-9 % 4 // equals -1
5.Floating-Point Remainder Calculations 浮点数求余
Swfit可以对浮点数进行求余计算,这是不同与其他语言的一个特点
8 % 2.5 // equals 0.5
6.Increment and Decrement Operators 自增和自增运算
对本身加1或减1的 ++ , -- 操作,可以是整型,也可以是浮点型
var i = 0
++i // i now equals 1
当++前置的时候,先自增再返回
当++后置的时候,先返回再自增
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
7.Unary Minus Operator/ Unary Plus Operator 负号和正号
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 // alsoMinusSix equals -6
8.Compound Assignment Operators 复合赋值
加赋运算( += ),复合赋值没有返回值,let b = a += 2 写法是错误的
var a = 1
a += 2 // a is now equal to 3
9.Comparison Operators 比较
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
let name = "world"
if name == "world" {
println("hello, world")
} else {
println("I'm sorry \(name), but I don't recognize you")
}
// prints "hello, world", because name is indeed equal to "world"
10.Ternary Conditional Operator 三元条件运算
三元条件运算是有三个操作数得运算符,question ? answer1 : answer2 ,如果question成立,返回answer1,否则返回answer2
let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)
// rowHeight is equal to 90
11.Range Operators 区间运算
闭区间(a...b),定义了从a到b(包括a和b)区间内的所有值
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
半闭区间(a..b),定义从a到b但不包括b的所有值,实用性在于当使用一个从0开始的数组,取数组的值
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
12.Logical Operators 逻辑运算
逻辑非 (!),对布尔值取反,前置操作符
let allowedEntry = false
if !allowedEntry {
println("ACCESS DENIED")
}
// prints "ACCESS DENIED"
逻辑与 (a&&b),只有a和b的值都为true,表达式的值才为true
let enteredDoorCode = true
let passedRetinaScan = false
if enteredDoorCode && passedRetinaScan {
println("Welcome!")
} else {
println("ACCESS DENIED")
}
// prints "ACCESS DENIED"
逻辑或 (a||b) , a和b的值其中有一个为true,表达式就为true
let hasDoorKey = false
let knowsOverridePassword = true
if hasDoorKey || knowsOverridePassword {
println("Welcome!")
} else {
println("ACCESS DENIED")
}
// prints "Welcome!"
组合
if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
println("Welcome!")
} else {
println("ACCESS DENIED")
}
// prints "Welcome!"
使用括号来明确运算优先级
if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
println("Welcome!")
} else {
println("ACCESS DENIED")
}
// prints "Welcome!"