有限状态自动机(FSM “finite state machine” 或者FSA “finite state automaton” )是为研究有限内存的计算过程和某些语言类而抽象出的一种计算模型。有限状态自动机拥有有限数量的状态,每个状态可以迁移到零个或多个状态,输入字串决定执行哪个状态的迁移。有限状态自动机可以表示为一个有向图。有限状态自动机是自动机理论的研究对象。
我们来看以下片段代码:1
2
3
4
5
6
7class ViewController {
var tableView: UITableView!
var errorLabel: UILabel!
var emptyStateLabel: UILabel!
var activityIndicator: UIActivityIndicatorView!
// Some implementation
}
我们得出4种状态,且每种独立存在:
- 显示数据
- 是否在加载数据中
- 是否显示错误
- 空状态
我们在控制器中定义该枚举:1
2
3
4
5
6
7
8extension ViewController {
enum State {
case loading
case showingData([Item])
case empty
case error(Error)
}
}