快看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 nilvar 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 // Jackfor 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!"?
轉載于:https://www.cnblogs.com/wuhuacong/p/8134440.html
總結
以上是生活随笔為你收集整理的快看Sample代码,速学Swift语言(3)-运算符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 100+经典Java面试题及答案解析
- 下一篇: hibernate的映射之二(一对多双向