ios - RxSwift - 无法推断通用参数 'Self'

标签 ios swift generics rx-swift

我有一个 UITableView 和一个 countries 变量,其签名如下:

let countryArray = ["Bangladesh", "India", "Pakistan", "Nepal", "Bhutan", "China", "Malaysia", "Myanmar", "Sri Lanka", "Saudi Arabia"]

当我试图在 UITableView 中绑定(bind)这个国家数组时,它显示错误 Generic parameter 'Self' could not be inferred

这是我正在做的片段:

let countries = Observable.just(countryArray)
    countries.bindTo(self.tableView.rx.items(cellIdentifier: "myCell",
                                        cellType: MyCell.self)) {
                                            row, country, cell in
                                            // configuring cell
    }
    .addDisposableTo(disposeBag)

最佳答案

我建议您使用最新版本的 RxSwift。您现在使用的内容已弃用。你的错误可能与它有关。

有两种方法可以完成您正在做的事情:

let countryArray = ["Bangladesh", "India", "Pakistan", "Nepal", "Bhutan", "China", "Malaysia", "Myanmar", "Sri Lanka", "Saudi Arabia"]
let countries = Observable.of(countryArray)

// Be sure to register the cell
tableView.register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "myCell")
  1. 要在 items(cellIdentifier:cellType:) 中提供单元格类型,这基本上就是您正在做的事情:

    countries
        .bind(to: tableView.rx.items(cellIdentifier: "myCell", cellType: MyCell.self)) { (row, element, cell) in
            // configure cell
        }
        .disposed(by: disposeBag)
    
  2. 提供细胞工厂闭包,换句话说,将闭包中的细胞出列并返回:

    countries
        .bind(to: tableView.rx.items) { (tableView, row, element) in
            let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: IndexPath(row: row, section: 0)) as! MyCell
            // configure cell
            return cell
        }
        .disposed(by: disposeBag)
    

两者各有利弊。第二个有对 tableView 的引用,这有时非常方便。

关于ios - RxSwift - 无法推断通用参数 'Self',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51524767/

相关文章:

ios - Concurrent NSOperation 以及如何设置 isFinished 和 isExecuting?

ios - 在 TableView 之外配置自定义 TableViewCell?

c# - 通过泛型方法传递类型的新实例

c# - 通用类型 - 名称可以简化

ios - 使用适用于 iOS 的 AddThis SDK,需要更改默认 Twitter 文本的 "via @AddThis"

ios - UIStepper - 从 NSUserDefaults 设置值

ios - Swift - 播放声音时出现错误 "fatal error: unexpectedly found nil while unwrapping an Optional value"

swift - 通过遵循数学数学规则对值进行舍入

c# - 如何在迭代时从通用列表中删除元素?

ios - EarlgreyUI iOS 测试中的导航问题