微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

void函数中的非预期非无效返回值Swift

如何解决void函数中的非预期非无效返回值Swift

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

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

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

 func UICollectionViewCell() {
 
 }
 

解决方法

您对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 ;
}
,
  1. 您需要添加到函数的返回值并删除私有关键字

    func collectionView(_ collectionView:UICollectionView, cellForItemAt indexPath:IndexPath)-> UICollectionViewCell { 让单元格= collectionView .dequeueReusableCell(withReuseIdentifier:“ a11”,用于:indexPath)

         return cell
    

    }

  2. 删除此代码

    func UICollectionViewCell(){

    }

  3. 检查此ID为“ a11”的单元是否已注册。 (在情节提要中或在代码中,具体取决于您的实现)。

请提供错误信息吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。