【问题标题】:unexpected non-void return value in void function (swift)void 函数中的意外非 void 返回值(swift)
【发布时间】:2020-08-17 23:36:01
【问题描述】:

我从下面的代码中收到以下错误。我不知道如何解决这个错误,也找不到有用的答案。

void 函数中出现意外的非 void 返回值

 private func collectionView( _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath){
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "a11", for: indexPath)
    
    return cell ;
 }

 func UICollectionViewCell() {
 
 }
 

【问题讨论】:

标签: swift


【解决方案1】:

func collectionView( _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) 的函数定义没有定义返回值,但您的函数返回的是 UICollectionViewCell。这就是问题的原因。此外,您可能也不希望将 cellForItemAt 设为私有方法。您应该将其更新为

func collectionView( _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "a11", for: indexPath)
    
    return cell ;
}

【讨论】:

    【解决方案2】:
    1. 您需要添加到您的函数返回值并删除一个私有关键字

      func collectionView(_collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 让单元格 = 集合视图 .dequeueReusableCell(withReuseIdentifier: "a11", for: indexPath)

           return cell
      

      }

    2. 删除此代码

      func UICollectionViewCell() {

      }

    3. 检查此 ID“a11”的单元格是否已注册。 (在故事板或代码中,取决于您的实现)。

    你能提供错误的文字吗?

    【讨论】:

      猜你喜欢
      • 2021-06-27
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多