【问题标题】:items' produces '(Source) -> Disposable', not the expected contextual result type '(_) -> _' error with UICollectionLayoutitems' 产生 '(Source) -> Disposable',而不是 UICollectionLayout 的预期上下文结果类型 '(_) -> _' 错误
【发布时间】:2023-03-07 17:56:02
【问题描述】:

我有一个包含 UICollectionView 的自定义控件。这个自定义控件有自己的视图模型。我正在使用 RXswift 进行 MVVM 设计。

视图模型类如下所示

class CalendarViewModel {

    public var calendarNDays = PublishSubject<[CalendarControlDayModel]>()

    struct CalendarControlDayModel {
        var date: String = ""
        var day: Int = 0
        var name: String = ""
    }

}

自定义控件称为 calendarView,我在其中创建上述视图模型的实例并设置绑定以在集合视图中填充数据。

在自定义控件中设置绑定的方法如下。

func setUpBinding() {

        calendarViewModel.calendarNDays.bind(to: collectionView.rx.items(cellIdentifier: cellReuseIdentifier, cellType: CalendarViewCell.self)) {  (row,calendarDay,cell) in
            cell.calendarDay = calendarDay
            }.disposed(by: disposeBag)

    }

但我收到上述方法的编译错误,显示为“items”生成“(Source) -> Disposable”,而不是预期的上下文结果类型“(_) -> _”

如果我在自定义控件中将 calendarNDays 定义为 publishSubject,它不会抱怨。关于这里有什么问题的任何提示。我想使用视图模型中定义的属性设置绑定,以显示集合视图项并符合 MVVM 设计模式。

【问题讨论】:

  • 顺便说一句 calendarNDays 应该是 let 而不是 var
  • 改为让。

标签: ios rx-swift


【解决方案1】:

我怀疑你已经定义了两个CalendarControlDayModel 类型。一个在CalendarViewModel 中,另一个在别处定义。检查并报告。

【讨论】:

  • 我只定义了一次。我搜索了解决方案并确保它只定义了一次。
  • 但仔细观察后发现 cell.calendarDay = calendarDay 不是导致问题的相同类型。修复 CalendarViewCell 中的数据类型。
  • 好。每次您看到该错误时,都意味着类型不同。只需弄清楚哪种类型是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多