【问题标题】:How to Fix the error: Generic parameter 'T' could not be inferred如何修复错误:无法推断通用参数“T”
【发布时间】:2019-06-10 17:44:37
【问题描述】:

我正在做一个教程,并且有一个错误问题:

无法推断通用参数“T”。

****>> 我只是按照您的建议更新问题。
并添加了 2 行代码:

let cell: TacoCell = collectionView.dequeReusableCell(forIndexPath: indexPath)
    cell.configureCell(taco: ds.tacoArray[indexPath.row])
    返回单元格

我也修改了这段代码:

函数 dequeReusableCell 到 dequeReusableCell
在我的 UICollectionView+Ex.swift 文件中。
import UIKit

extension UICollectionView {
func register<T: UICollectionViewCell>(_: T.Type) where T: ReusableView, T: NibLoadableView {

    let nib = UINib(nibName: T.nibName, bundle: nil)
    register(nib, forCellWithReuseIdentifier: T.reuseIdentifier)

}

func dequeReusableCell<T: UICollectionView>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView{
    guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath as IndexPath) as? T else {
        fatalError("Coud not deque cell with Identifier: \(T.reuseIdentifier)")
    }
    return cell
}

}

extension UICollectionViewCell: ReusableView {}

在我的 MainVC.swift 中,我有以下代码:

import UIKit

class MainVC: UIViewController, DataServiceDelegate {

@IBOutlet weak var headerView: HeaderView!
@IBOutlet weak var collectionView: UICollectionView!

var ds: DataService = DataService.instance

override func viewDidLoad() {
    super.viewDidLoad()

    ds.delegate = self
    ds.loadDeliciousTacoData()

    collectionView.dataSource = self
    collectionView.delegate = self

    headerView.addDropShadow()
    /*
    let nib = UINib(nibName: "TacoCell", bundle: nil)
    collectionView.register(nib, forCellWithReuseIdentifier: "TacoCell")
    */
    collectionView.register(TacoCell.self)
}

func deliciousTacoDataLoaded() {
   print("Delicious Taco Data Loaded!")
}


}

extension MainVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return ds.tacoArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TacoCell", for: indexPath) as? TacoCell {
//            cell.configureCell(taco: ds.tacoArray[indexPath.row])
//            return cell
//        }
//        return UICollectionViewCell()

    let cell: TacoCell = collectionView.dequeReusableCell(forIndexPath: indexPath)
    cell.configureCell(taco: ds.tacoArray[indexPath.row])
    return cell

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 95, height: 95)
}

}

在这一行:

let cell = collectionView.dequeReusableCell(forIndexPath: indexPath) as TacoCell

编译器抱怨。

为什么会显示此错误,以及如何解决?预先感谢。
==> 听从你的建议,现在我可以成功了。 我的问题解决了。我还在实现一些东西,如果有更多任何问题,我会再次更新。非常感谢!

【问题讨论】:

  • 您的通用约束显示为&lt;T: UICollectionView&gt;,而它应该是&lt;T: UICollectionViewCell&gt;
  • @totiG 如果您使用类型注释,则不会发生类型推断,因为您明确告诉编译器(您认为)应该是什么类型 :) 仍然会进行类型检查,因此类型无效注释不起作用。
  • 如果您可以编辑您的问题并包含最少的相关代码,那就太好了。您的许多代码与该问题无关。这样您就可以更快地得到答案...
  • @dan 感谢您的帮助,我将 修改为 并解决了一个问题。

标签: ios swift generics


【解决方案1】:

您应该让dequeReusableCell 的通用约束为:&lt;T: UICollectionViewCell&gt; 而不是&lt;T: UICollectionView&gt;

func dequeReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView{

显然,我认为TacoCellReusableView 的类型。

【讨论】:

    【解决方案2】:
    1. 修正错别字

      func dequeueReusableCell<T: UICollectionViewCell>( ...
      
    2. 注解类型并移除类型转换

      let cell : TacoCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
      

    【讨论】:

    • 我不认为#2 是必要的,编译器可以根据as cast 推断出通用返回类型
    • @vadian 非常感谢,因为您多次帮助了我。你的解决方案是正确的。我的问题解决了。
    • @dan 我又试了一次,像你说的那样不需要#2。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多