《The Swift Programming Language》学习疑问

1 构造过程、析构过程

  1. 便利构造器的作用

    提取出多个构造器之间重复的代码

  2. 自动继承的影响

    注意自动继承的两种情况,在遇到问题时能很快了解问题根源

  3. Init! 可失败构造器

    尽量用init?代替init!,因为在init!构造失败时程序会崩溃,在线环境下影响较大。

    于此类似,解包时也尽量用?代替!,用as?代替as!

  4. 什么情况会自定义析构器

    很少有情况会自定义析构器

2 协议、泛型

  1. 有了类型参数,为什么要需要关联类型?关联类型的使用场景?

    只有协议有关联类型,因为协议没法使用泛型,所以可以用关联类型来代替泛型

  2. 返回的类型选择不透明类型还是协议类型?是否返回不透明类型的情况更常见?

    项目中只有涉及到swift-ui才会用到不透明类型,返回协议类型的情况更常见

  3. unowned会导致运行时崩溃?

    尽量使用weak而不是unowned

3 Swift语言难点

1)Never

无实例类型,没有任何值,声称某个事件永远不会发生

2)reduce

对集合中的每个元素进行给定的闭包操作,闭包的参数为上一次的运算结果和集合中的下一个元素

Declaration

1
func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, SectionProcessor) throws -> Result) rethrows -> Result

Discussion

Use the reduce(_:_:) method to produce a single value from the elements of an entire sequence.

The nextPartialResult closure is called sequentially with an accumulating value initialized to initialResult and each element of the sequence.

1
2
3
4
5
let numbers = [1, 2, 3, 4]
let numberSum = numbers.reduce(0, { x, y in
x + y
})
// numberSum == 10

When numbers.reduce(_:_:) is called, the following steps occur:

  1. The nextPartialResult closure is called with initialResult0 in this case—and the first element of numbers, returning the sum: 1.
  2. The closure is called again repeatedly with the previous call’s return value and each element of the sequence.
  3. When the sequence is exhausted, the last value returned from the closure is returned to the caller.

If the sequence has no elements, nextPartialResult is never executed and initialResult is the result of the call to reduce(_:_:).

3)if语句中的逗号

在条件语句中使用&&时,由于取可选值的返回结果不是Bool,因此不能用&&连接,可以用逗号表示相同的与运算

4)as as! as?

as:

  1. 向上转型,派生类转基类
  2. 数值类型转换,消除二义性
  3. switch语句中的模式匹配

as!:向下转型,失败会报runtime错误

as?:向下转型,失败会返回nil,成功返回optional类型

5)typealias

类型别名

要使用的名字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

## 6)fileprivate

swift中的访问控制权限:private,fileprivate,internal,public 和 open

1. private:只能在当前类的作用域使用
2. fileprivate:能在当前文件内访问到,不管是否在本类的作用域
3. internal:默认的,可被同一程序集中的所有类访问(同一程序集:被编译到同一dll或exe的程序)
4. public:在别的框架中只能使用,无法继承和重写
5. open:在别的框架中可以继承和重写

## 7)fileprivate(set)

```swift
fileprivate(set) var cost: UInt = 0 //给cost变量添加set方法

8)== 和 ===

== :比较基本类型,只有实现Equatable才能比较引用类型

=== :比较两个对象是否完全一样,不能比较基本类型

9)subscript

下注脚本,允许使用[ ]来访问数组或者字典

语法

1
2
3
4
5
6
7
8
subscript(index: Int) -> Int {
get {
// 返回与入参匹配的Int类型的值
}
set(newValue) {
// 执行赋值操作
}
}

10)让一个协议继承Anyobject

表明遵循该协议的类型只能是calss,不能是struct和enum,这样就可以使用weak

​ let arrayGenerateException: [Int] = [0]

​ let num1 = arrayGenerateException[1]

​ NSLog(“(num1)”)

4 RxSwift

1) PublishSubject

将对观察者发送订阅后产生的元素,而在订阅前发出的元素将不会发送给观察者。

如果你希望观察者接收到所有的元素,你可以通过使用 Observablecreate 方法来创建 Observable,或者使用 ReplaySubject

2) BehaviorSubject

当观察者对 BehaviorSubject 进行订阅时,它会将源 Observable 中最新的元素发送出来(如果不存在最新的元素,就发出默认元素)。

然后将随后产生的元素发送出来。

3) combineLatest

多个 Observables 中任何一个发出一个元素,就发出一个元素

4) map

将源 Observable 的每个元素应用你提供的转换方法,然后返回含有转换结果的 Observable

5) merge

将多个 Observables 合并成一个,当某一个 Observable 发出一个元素时,他就将这个元素发出。

如果,某一个 Observable 发出一个 onError 事件,那么被合并的 Observable 也会将它发出,并且立即终止序列。

0%