【问题标题】:Bind event to key of Observable<JSON>将事件绑定到 Observable<JSON> 的键
【发布时间】:2018-10-09 00:46:26
【问题描述】:

我有一个返回 2 个可观察事件的函数,第一个事件有一个键 athletes,我想从中创建一个表。我怎样才能做到这一点?

我得到的可能错误是:

  • Cannot subscript a value of incorrect or ambiguous type
  • Value of type 'JSON' has no member 'asObservable'

    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    teamRequest(schoolID: self.schoolID).debug("val").elementAt(1)
        .bind(to: self.tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)) { row, element, cell in
        cell.textLabel?.text = element["Name"]
    }.disposed(by: disposeBag)
    

其中 teamRequest 返回一个 JSON 类型的 observable,发出 2 个值。

【问题讨论】:

  • “JSON”类型到底是什么? 'teamRequest(schoolID: self.schoolID)' 返回什么?

标签: swift uitableview observable rx-swift swifty-json


【解决方案1】:

为了使您发布的代码能够正常工作,teamRequest(schoolID: self.schoolID) 必须返回一个数组。会这样吗?

我不确定你认为你在用elementAt 运算符做什么。在典型的 RxSwift 代码中,这是一个非常罕见的运算符。你确定你需要它吗?

【讨论】:

    【解决方案2】:

    绑定到表只适用于数组类型的可观察对象(即'Observable')。 如果您需要按键从 observable 中进行选择,请使用 '.groupBy()' 运算符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多