以下来自RxSwift/RxCocoa的示例代码是做什么的?
我正在努力了解细节以下来自RxSwift/RxCocoa的示例代码是做什么的?,swift,rx-swift,Swift,Rx Swift,我正在努力了解细节 .drive(resultsTableView.rx_itemsWithCellIdentifier("WikipediaSearchCell", cellType: WikipediaSearchCell.self)) { (_, viewModel, cell) in cell.viewModel = viewModel } 来自WikipediaSearchViewController
.drive(resultsTableView.rx_itemsWithCellIdentifier("WikipediaSearchCell",
cellType: WikipediaSearchCell.self))
{ (_, viewModel, cell) in
cell.viewModel = viewModel
}
来自WikipediaSearchViewController.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)
给予
无法使用类型为“(字符串,cellType:UITableViewCell.type)”的参数列表调用“rx_itemsWithCellIdentifier”
对于驱动器arg1和
表达式类型不明确,没有更多上下文
对于驱动器arg2
drive
和rx\u 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 {}
public func drive(带:Self->R1->R2,当前版本:R1)->R2{}
public func rx_itemsWithCellIdentifier(cellIdentifier:String,cellType:Cell.Type=Cell.self)(源:O)(configureCell:(Int,S.Generator.Element,Cell)->Void)->一次性{}
但在这一点上,Swift的语法对我来说是难以理解的。有人能解释签名和代码中发生了什么吗?在这里,由于缺乏上下文,Swift编译器无法推断
driverg1
和driverg2
的类型。当在drive()
调用中内联使用时,编译器对于每个参数的类型有更多的线索,我们最终不需要对这些类型进行注释
考虑到这一点,让我们尝试为这两个变量添加类型注释
首先,我们将使用swift 2.2更新rx\u itemsWithCellIdentifier
的签名,删除令人困惑的curry语法并添加通用注释
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, S.Generator.Element, Cell) -> Void)
-> Disposable
为了简化接下来的操作,让我们为它定义一个typealias
typealias CellConfigurator = (Int, SearchResultViewModel, WikipediaSearchCell) -> Void
我们现在可以定义driverarg2
let driveArg2: CellConfigurator = { (_, viewModel: SearchResultViewModel, cell: WikipediaSearchCell) in
cell.viewModel = viewModel
}
驱动器arg1的类型
既然driverg2
已经过时了,那么确定driverg1
的类型就变得更容易了。它只是rx\u itemsWithCellIdentifier
的返回类型,替换了泛型部分
typealias DriveArg2Type = (source: Observable<[SearchResultViewModel]>) -> (CellConfiguration) -> Disposable
我知道存在某种泛型类型不匹配。而且它无法在rx_itemsWithCellIdentifier函数参数中计算出(S:SequenceType)的泛型类型。
typealias DriveArg2Type = (source: Observable<[SearchResultViewModel]>) -> (CellConfiguration) -> Disposable
drive(Self -> R1 -> R2, curriedArgument: R1) -> R2
// where
Self = Observable<[SearchResultViewModel]>
R1 = CellConfigurator
R2 = Disposable