快看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!"