首页 > 解决方案 > Swift 中的表达式类型不明确

问题描述

我有以下使用RxSwift的代码:

 self.photos
                .bind(to: collectionView.rx.items(dataSource: self.dataSource))
                .disposed(by: disposeBag)

它给了我Type of expression is ambiguous without more context

它还需要什么上下文?

完整代码如下所示:

//
//  PhotosCollectionViewController.swift
//  TodoListRxSwift
//

//

import Foundation
import UIKit
import RxSwift
import RxCocoa
import RxDataSources

struct Photo  {
    var name :String
    var imageURL :String
}

struct SectionOfPhoto {
    var header: String
    var items: [Photo]
}
extension SectionOfPhoto: SectionModelType {

    init(original: SectionOfPhoto, items: [Photo]) {
        self = original
        self.items = items
    }
}

class PhotosCollectionViewController :UICollectionViewController {

    private let disposeBag = DisposeBag()

    private (set) var photos = BehaviorRelay(value: [Photo(name: "Pic 1", imageURL: "1.png"),Photo(name: "Pic 2", imageURL: "2.png"),Photo(name: "Pic 3", imageURL: "3.png")])

    let dataSource = RxCollectionViewSectionedReloadDataSource<SectionOfPhoto>(configureCell: { ds, cv, indexPath, photo in

        let cell = cv.dequeueReusableCell(withReuseIdentifier: "PhotoCollectionViewCell", for: indexPath)

        return cell

    })

    override func viewDidLoad() {

        super.viewDidLoad()

        self.collectionView?.delegate = nil
        self.collectionView?.dataSource = nil

        configureObservables()
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        prepareSegueForAddPhotoViewController(segue :segue)
    }

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        switch kind {
            case UICollectionElementKindSectionHeader:

                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "PhotosHeaderView", for: indexPath)
                return headerView

            default:
                return UICollectionReusableView()
        }

    }

    private func prepareSegueForAddPhotoViewController(segue :UIStoryboardSegue) {

        guard let nc = segue.destination as? UINavigationController else {
            fatalError("NavigationController does not exist")
        }

        guard let addPhotoVC = nc.topViewController as? AddPhotoViewController else {
            fatalError("AddPhotoViewController does not exist")
        }

        _ = addPhotoVC.selectedPhoto.subscribe(onNext: { (photo) in
           self.photos.accept(self.photos.value + [photo])
        })
    }


    private func configureObservables() {

        if let collectionView = self.collectionView {

            self.photos.bind(to: collectionView.rx.items(dataSource: self.dataSource))

            self.photos.bind(to: collectionView.rx.items(cellIdentifier: "PhotoCollectionViewCell", cellType: PhotoCollectionViewCell.self)) { row, model, cell in

                cell.photoImageView.image = UIImage(named: model.imageURL)

            }.disposed(by: disposeBag)

        }
    }
}

标签: iosswiftrx-swift

解决方案


问题是那photos是错误的类型。数据源需要 的元素[SectionOfPhoto],但photos有 的元素[Photo]

但是,更改/修复照片类型会中断addPhotoVC.selectedPhoto,因为它试图将单张照片添加到一组部分。


推荐阅读