比较运算符

  • Swift中提供了检查两个对象是否同引用同一个示例的操作符===!==

  • 当两个元祖有相同的元素类型和个数时,可以直接比较两个元祖。从左到右,直到找到不相等的。

    1
    2
    3
    
    (1, "zebra") < (2, "apple")   // true because 1 is less than 2; "zebra" and "apple" are not compared
    (3, "apple") < (3, "bird")    // true because 3 is equal to 3, and "apple" is less than "bird"
    (4, "dog") == (4, "dog")      // true because 4 is equal to 4, and "dog" is equal to "dog”

Nil-Coalescing

用法

1
a ?? b

当可选值a包含值的时候,上面的表达式返回a的值,当anil时,否则返回b

等价于三目运算符

1
a != nil ? a!: b

使用这个运算符的好处就是在处理可选值时,不用强制拆包

例如

1
2
3
4
5
6
7
8
9
let defaultColorName = "red"
var userDefinedColorName: String?

var colorNameToUse = userDefinedColorName ?? defaultColorName
print(colorNameToUse)  // red

userDefinedColorName = "green"
colorNameToUse = userDefinedColorName ?? defaultColorName
print(colorNameToUse) // green

范围运算符

1
2
3
4
5
6
7
8
9
// Closed Range Operator
// 全闭包,包含`a`和`b`, 必须满足`a`<=`b`
(a...b)

// Half-Open Range Operator
//半闭包,包含`a`, 但是不包含`b`
(a..<b)

//one-sided range
 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
// Closed Range Operator
for index in 1...5 {
    print(index, terminator: ",") // 1,2,3,4,5,
}

let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count

// Half-Open Range Operator
for index in 0..<count {
    print(names[index], terminator: ",") // Anna,Alex,Brian,Jack,
}

// one-sided range
for name in names[2...] {
    print(name, terminator: ",") // Brian,Jack,
}

for name in names[..<2] {
    print(name, terminator: ",") // Anna,Alex,
}


// 检查是否包含特定的值
let range = ...5
range.contains(4)

逻辑运算符

  • && 逻辑与,短路运算,钱一个值为false时,直接返回false, 不会计算后面的表达式
  • || 逻辑或
  • !a 非运算

当有多个&&||运算时,他们的优先级是从左到右的。这种情况下最好使用括号来明确优先级顺序

字符(Characters)和字符串(String)

  • 字符串只能使用双引号""
  • 多行字符串可以用三引号"""
  • 多行字符串缩进效果如下图所示

  • isEmpty属性可以判断一个字符串是否为空

    1
    2
    3
    4
    5
    
    // 注意是属性,不是方法
    var emptyString = ""
    if emptyString.isEmpty {
    print("Nothing to see here")
    }
  • 声明字符

通过Character类型声明字符

1
let character: Character = "a"
  • 字符串也可以由字符串数组来声明

    1
    2
    3
    4
    5
    
    let catCharacter: [Character] = ["C", "a", "t", "!"]
    
    let catString = String(catCharacter)
    
    print(catString) // "Cat!"
  • 字符和字符串拼接

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    // 用 + 号拼接字符串
    let string1  = "Hello "
    let string2 = "World"
    var welcome = string1 + string2
    print(welcome)  // "Hello World"
    
    // 用append方法添加字符到字符串
    
    let exclamationMark:Character = "!"
    
    welcome.append(exclamationMark)
    
    print(welcome) // "Hello World!"
  • 统计字符的个数

使用count属性,可以求一个字符串中的字符个数

1
2
let foo = "ABCDEFG"
print(foo.count) // 7
  • 字符串索引

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    let foo = "ABCDEFGHIJKLMN"
    print(foo[foo.startIndex]) // A
    
    // 从这里看出swift是否支持函数重载,两个函数只是参数名字不一样
    print(foo[foo.index(before: foo.endIndex)]) // N
    print(foo[foo.index(after: foo.startIndex)])  // B
    
    let index = foo.index(foo.startIndex, offsetBy: 7)
    print(foo[index])  // H
    
    // 通过indices属性可以遍历每个字符
    for index in foo.indices{
    print(foo[index], terminator: "") // ABCDEFGHIJKLMN
    }
  • 字符串插入和删除操作

使用insert(_:at:)insert(contentsOf:at:)插入字符和字符串 使用remove(at:)removeSubrange(_:)删除字符和字符串

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var welcome = "hello"

welcome.insert("!", at: welcome.endIndex) // welcome变成 "hello!"

welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex)) // welcome变成"hello there!"


// remove方法会返回被移除的字符
welcome.remove(at: welcome.index(before:welcome.endIndex)) // 返回值是"!",welcome变成 "hello there"

let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex

welcome.removeSubrange(range) // welcome变成 "hello"
  • 子字符串

在Swift中,当从一个字符串中获取字符串时,得到的是一个Substring类型,并不是一个新的字符串,Substring和字符串类型的方法完全相同,唯一的区别就是子字符串只会在操作字符串时短暂保存,当需要长期使用子字符串时,应该将它转换为字符串类型

1
2
3
4
5
6
7
8
9
let greeting = "Hello, world!"

let index = greeting.firstIndex(of: ",") ?? greeting.endIndex

let beginning = greeting[..<index]
// beginning is "Hello"

// convert the result to string for long-term storage.
let newString = String(beginning)

因此子字符中类型并不适合长期保存。

  • 字符串前缀和后缀

    1
    2
    3
    
    let msg = "Hello World"
    msg.hasPrefix("Hello") // true
    msg.hasSuffix("World") // true