快看Sample代码,速学Swift语言(3)-运算符
运算符是用来检查,更改或组合值的特殊符号或短语。Swift提供的很多常规的运算符,如+、-、*、/、%、=、==等,以及逻辑运算的&&、||等等,基本上不需要重复介绍,我们在这里只需要了解一些不太一样的运算符就可以了。如Swift引入的新运算符,范围操作符号,包括..<和...两个,该随笔介绍Swift常规的运算符中,以及和其他语言有所差异的部分。
赋值运算符
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
这种代码是类似ECMAScript 6的脚本写法,通过把右边元祖对象解构赋值给左边对应的参数。
数学运算符
1 + 2 // equals 3 5 - 3 // equals 2 2 * 3 // equals 6 10.0 / 2.5 // equals 4.0
这些都是和其他语言没有什么不同,循例列出参考下
对于字符,也可以使用+符号进行连接新的字符串
"hello, " + "world" // equals "hello, world"
一元操作符中的-、+运算,和算术里面的负负得正,正负得负的意思一样了。
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
组合运算符提供+= 、-=的运算符操作
var a = 1 a += 2 // a is now equal to 3
对比运算符和其他语言差不多
等于 (
a == b
)不等于 (
a != b
)大于 (
a > b
)小于 (
a < b
)大于等于 (
a >= b
)小于等于 (
a <= b
)
另外值得注意的是,Swift提供了对比引用的两个操作符号,
===
和
!==,用来检查两个引用是否完全相等;或者不相等的。而==只是用来对比两个对象的值是否一致。
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
对比运算符也经常用来If条件语句里面
let name = "world" if name == "world" { print("hello, world") } else { print("I'm sorry \(name), but I don't recognize you") } // Prints "hello, world", because name is indeed equal to "world".
三元运算符
三元运算符 ? :和C#里面表现是一样的
question ? answer1 : answer2
let contentHeight = 40 let hasHeader = true let rowHeight = contentHeight + (hasHeader ? 50 : 20)
空值转换操作符
空值转换符是对可空类型(可选类型)的一个值得转换出来(
a ?? b
)。
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"
范围操作符
闭合范围运算符 ... 和半闭合范围运算符 ..< 两个
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
半闭合的范围运算符
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) } // Anna // Alex
以及一侧范围的运算符,包括左侧和右侧两个部分
for name in names[2...] { print(name) } // Brian // Jack for name in names[...2] { print(name) } // Anna // Alex // Brian
let range = ...5 range.contains(7) // false range.contains(4) // true range.contains(-1) // true
逻辑运算符
let allowedEntry = false if !allowedEntry { print("ACCESS DENIED") } // Prints "ACCESS DENIED"
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!"