RxSwift / RxCocoa的以下示例代码是做什么的?
|
我想详细了解
.drive(resultsTableView.rx_itemsWithCellIdentifier("WikipediaSearchCell",cellType: WikipediaSearchCell.self))
{ (_,viewModel,cell) in
cell.viewModel = viewModel
}
来自维基百科SearchViewController.swift第47-64行. let temp1 = searchBar.rx_text
.asDriver()
.throttle(0.3)
.distinctUntilChanged()
.flatMapLatest { query in
API.getSearchResults(query)
.retry(3)
.retryOnBecomesReachable([],reachabilityService: ReachabilityService.sharedReachabilityService)
.startWith([]) // clears results on new search term
.asDriver(onErrorJustReturn: [])
}
.map { results in
results.map(SearchResultViewModel.init)
}
let driveArg1 = resultsTableView.rx_itemsWithCellIdentifier("WikipediaSearchCell",cellType: WikipediaSearchCell.self)
let driveArg2 = { (_,viewModel: SearchResultViewModel,cell: WikipediaSearchCell) in
cell.viewModel = viewModel
}
temp1.drive(driveArg1,curriedArgument: driveArg2)
.addDisposableTo(disposeBag)
给
对于driveArg1和
对于driveArg2. 驱动器和rx_itemsWithCellIdentifier的签名是 public func drive<R1,R2>(with: Self -> R1 -> R2,curriedArgument: R1) -> R2 {}
public func rx_itemsWithCellIdentifier(cellIdentifier: String,cellType: Cell.Type = Cell.self)(source: O)(configureCell: (Int,S.Generator.Element,Cell) -> Void) -> Disposable {}
但在这一点上,Swift语法对我来说是不可理解的.任何人都可以解释签名,代码中会发生什么?
在这里,由于缺少上下文,Swift编译器无法推断出driveArg1和driveArg2的类型.当在drive()调用中使用内联时,编译器有更多的线索,每个参数的类型可以是什么,我们最终不需要为这些类型注释.
考虑到这一点,我们尝试为这两个变量添加类型注释. 首先,我们将使用swift 2.2更新rx_itemsWithCellIdentifier的签名,从而消除令人困惑的currying语法,并添加通用注释 public func rx_itemsWithCellIdentifier
<S: SequenceType,Cell: UITableViewCell,O : ObservableType where O.E == S>
(cellIdentifier: String,cellType: Cell.Type = Cell.self)
-> (source: O)
-> (configureCell: (Int,Cell) -> Void)
-> Disposable
driveArg2的类型 这是我们传递给curriedArgument的驱动()的参数,并且将是在应用(source:O)之后传递给rx_itemsWithCellIdentifier的参数.因此,它需要匹配(Int,Cell) – >空虚 这种类型定义中有两个未知的,S.Generator.Element和Cell.它们是通用的,所以我们需要弄清楚它们是什么. > Cell很容易,它是我们要配置的单元格的类型,这里是WikipediaSearchCell. 好,我们现在有driverArg2的签名: (Int,SearchResultViewModel,WikipediaSearchCell) -> Void 为了简化接下来的内容,我们为它定义一个typealias typealias CellConfigurator = (Int,WikipediaSearchCell) -> Void 我们现在可以定义driveArg2 let driveArg2: CellConfigurator = { (_,cell: WikipediaSearchCell) in
cell.viewModel = viewModel
}
driveArg1的类型 现在,driveArg2已经失败了,找出driveArg1的类型变得更加容易.它只是rx_itemsWithCellIdentifier的返回类型,替换了通用部分 typealias DriveArg2Type = (source: Observable<[SearchResultViewModel]>) -> (CellConfiguration) -> Disposable 驱动签名 随着所有这一切的扩大,驱动器的类型签名希望更有意义: drive(Self -> R1 -> R2,curriedArgument: R1) -> R2 // where Self = Observable<[SearchResultViewModel]> R1 = CellConfigurator R2 = Disposable (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
