2017-03-24 4 views
1

である私のコードスニペット:いいえ」アイテムの候補者が予想されるコンテキスト結果型生産 '(観測可能<[Product]>を) - >(_) - > _' ここで

class ProductCategoryCell: UITableViewCell { 

    @IBOutlet weak var collectionViewProducts: UICollectionView! 

    // other stuff... 

    func setProducts() { 
    let productsObservable = Observable.just([ 
     Product(name: "test", price: 10.0), 
     Product(name: "test", price: 10.0), 
     Product(name: "test", price: 10.0) 
    ]) 

    productsObservable.bindTo(collectionViewProducts.rx.items(cellIdentifier: "ProductCell", cellType: ProductCell.self)) { 
     (row, element, cell) in 
     cell.setProduct(element) 
    }.disposed(by: disposeBag) 
    } 
} 

それは私にビルドエラーを与えている。

私のビューコントローラで

No 'items' candidates produce the expected contextual result type '(Observable<[Product]>) -> (_) -> _'

、私は同様のコードを使用してテーブルビューを移入しています:

let productsObservable = Observable.just(testProducts) 
productsObservable.bindTo(tableViewProducts.rx.items(cellIdentifier: "ProductCategoryCell", cellType: ProductCategoryCell.self)) { (row, element, cell) in 
     cell.setCategory(category: element) 
}.disposed(by: disposeBag) 

このコードは正常に動作します ために。私は間違って何をしていますか?

答えて

4

取得しているエラーメッセージを再現できました。 ProductCellsetProduct()メソッドのコードは投稿しませんでしたが、同じ問題が発生している可能性があります。私はあなたの答えでコードを使用するときに今

class ProductCell: UICollectionViewCell { 
    func setProduct(product: Product) { 
     // do stuff 
    } 
} 

productsObservable.bindTo(collectionViewProducts.rx.items(cellIdentifier: "ProductCell", cellType: ProductCell.self)) { (row, element, cell) in 
    cell.setProduct(element) 
} 
.disposed(by: disposeBag) 

私はあなたよりも、同じエラーが表示されます。

は、これは私のダミーProductCell実装です

No 'items' candidates produce the expected contextual result type '(Observable<[Product]>) -> (_) -> _'

私の場合、問題は、引数のラベルproductを忘れてしまったときでした。エラーなしでコードがコンパイル引数のラベルを追加した後

setProduct:を呼び出す:

productsObservable.bindTo(collectionViewProducts.rx.items(cellIdentifier: "ProductCell", cellType: ProductCell.self)) { (row, element, cell) in 
    cell.setProduct(product: element) 
} 
.disposed(by: disposeBag) 

コンパイラのエラーメッセージは、この場合には非常に紛らわしいです。

Missing argument label 'product:' in call

を何とかコンパイラは、彼がクロージャ内にあるとき、正確な問題が何であるかを認識していない:あなたが閉鎖のsetProduct(element)外を呼び出すようにしようとするとき、あなたは正しいエラーメッセージになるだろう。

私は私はあなたがあなたのProductCellsetProductを実装方法を知りませんが、あなたはUITableView例でcell.setCategory(category: element)を呼び出しているので、私はあなたの問題はあなたがcell.setProduct(element)

を呼び出すときに欠落している引数のラベルであることを前提と前に述べたように
関連する問題