简体   繁体   English

表达式的类型在Swift中不明确

[英]Type of expression is ambiguous in Swift

I have the following code using RxSwift : 我有以下代码使用RxSwift

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

And it gives me Type of expression is ambiguous without more context 它给我Type of expression is ambiguous without more context

What more context does it need? 还需要什么上下文?

The complete code is shown below: 完整的代码如下所示:

//
//  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)

        }
    }
}

The problem is that photos is the wrong type. 问题是photos类型错误。 The data source is expecting an element of [SectionOfPhoto] , but photos has an element of [Photo] . 数据源期望使用[SectionOfPhoto]元素,但是photos具有[Photo]元素。

However, changing/fixing the type of photos will break addPhotoVC.selectedPhoto because it's trying to add a single photo to an array of sections. 但是,更改/固定照片类型会破坏addPhotoVC.selectedPhoto因为它试图将单个照片添加到部分数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM