快看Sample代码,速学Swift语言(3)-运算符

运算符是用来检查,更改或组合值的特殊符号或短语。Swift提供的很多常规的运算符,如+、-、*、/、%、=、==等,以及逻辑运算的&&、||等等,基本上不需要重复介绍,我们在这里只需要了解一些不太一样的运算符就可以了。如Swift引入的新运算符,范围操作符号,包括..<和...两个,该随笔介绍Swift常规的运算符中,以及和其他语言有所差异的部分。

 

赋值运算符

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

 赋值语句,处理和其他语言一样。

 

1
2
let (x, y) = (1, 2)
// x is equal to 1, and y is equal to 2

 这种代码是类似ECMAScript 6的脚本写法,通过把右边元祖对象解构赋值给左边对应的参数。

 

数学运算符

1
2
3
4
1 + 2       // equals 3
5 - 3       // equals 2
2 * 3       // equals 6
10.0 / 2.5  // equals 4.0

 这些都是和其他语言没有什么不同,循例列出参考下

对于字符,也可以使用+符号进行连接新的字符串

1
"hello, " + "world"  // equals "hello, world"

 

一元操作符中的-、+运算,和算术里面的负负得正,正负得负的意思一样了。

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

 

对比运算符和其他语言差不多

  • 等于 (a == b)

  • 不等于 (a != b)

  • 大于 (a > b)

  • 小于 (a < b)

  • 大于等于 (a >= b)

  • 小于等于 (a <= b)

另外值得注意的是,Swift提供了对比引用的两个操作符号,=== 和 !==,用来检查两个引用是否完全相等;或者不相等的。而==只是用来对比两个对象的值是否一致。

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

 对比运算符也经常用来If条件语句里面

1
2
3
4
5
6
7
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#里面表现是一样的

1
question ? answer1 : answer2

 

1
2
3
let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)

 

空值转换操作符

空值转换符是对可空类型(可选类型)的一个值得转换出来(a ?? b)。

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

 

1
2
3
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 {
    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

 半闭合的范围运算符

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

 或者如下使用

1
2
3
4
5
for name in names[..<2] {
    print(name)
}
// Anna
// Alex

 

以及一侧范围的运算符,包括左侧和右侧两个部分

1
2
3
4
5
6
7
8
9
10
11
12
for name in names[2...] {
    print(name)
}
// Brian
// Jack
  
for name in names[...2] {
    print(name)
}
// Anna
// Alex
// Brian

 

1
2
3
4
let range = ...5
range.contains(7)   // false
range.contains(4)   // true
range.contains(-1// true

 

逻辑运算符

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

 

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

 

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

 

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

或者使用括号使之更加方便阅读

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

 

posted on   伍华聪  阅读(892)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2009-12-28 使用NVelocity自动生成Favorite收藏夹的导航页面

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示