文章目录
  1. 1. Swift学习-02基础运算符
    1. 1.1. 基础运算符
    2. 1.2. 聚合运算符
    3. 1.3. 区间运算符和for-in循环
    4. 1.4. 逻辑运算符

Swift学习-02基础运算符

基础运算符

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//------------------2.1基础运算符---------------//
/**
a + b +a a++ a += 2
a - b -b b-- a -= 2
a * b a *= 2
a / b a /= 2
a % b a %= 2
*/
let a = 5
let b = 2
var c = 0
a + b
a - b
a * b
a / b
a % b
let d = 5.2, e = 1.7
d / e
d % e
Int.max
Int.min
c++
c
++c
c
c += 2
c -= 2
c /= 2
c %= 2
/*
if question{
answer1
} ====》question?answer1:answer2
else{
answer2
}
*/
//状态栏高度
let statusBarHeight = 20
//iphone5基础视图高度
let basicViewHeight = 548
//是否全屏
var inFullScreenMode = true
//全屏高度
var viewHeight = basicViewHeight + ( inFullScreenMode ? statusBarHeight : 0 )
var score = 58
var rate = score >= 60 ? "及格" : "不及格"

聚合运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//------------------2.2nil聚合运算符------------//
//Nil Coalescing Operator
//针对可选型解包时使用
//a ?? b =====》 a != nil ? a! : b
//要求:a是可选型,b的类型和a解包后的类型是一致
var userNickName:String?
userNickName = "jackChang"
if userNickName != nil
{
println("Hello, \(userNickName)")
}
else{
println("Hello, Guest")
}
//三元运算符
let outputName:String = userNickName != nil ? userNickName! : "Guest"
println("Hello, " + outputName)
//nil聚合运算符
let outputName2:String = userNickName ?? "Guest"
println("Hello, " + outputName2)

区间运算符和for-in循环

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
//------------2.3区间运算符和for-in循环------------//
/**
* Range Operator
* Closed Range Operator
* 闭区间 [a,b] ====> a...b
*
* Half-Closed Range Operator
* 前闭后开区间 [a,b) ====> a..<b
**/
for index in 1...10
{
index
}
for index2 in 1..<10
{
index2
}
let names = ["jackChang", "QingU","Haha"]
for i in 0 ..< names.count
{
names[i]
}

逻辑运算符

1
2
3
4
5
6
7
//-------------------2.4 逻辑运算符--------------------//
/**
* Logical Operator
* !a
* a && b
* a || b
*/
文章目录
  1. 1. Swift学习-02基础运算符
    1. 1.1. 基础运算符
    2. 1.2. 聚合运算符
    3. 1.3. 区间运算符和for-in循环
    4. 1.4. 逻辑运算符