【发布时间】:2018-11-09 04:25:25
【问题描述】:
我在一个UIViewController 中有三个UICollectionView。然后我为每个UICollectionView设置IBOutlet
@IBOutlet weak var firstCollectionView: UICollectionView!
@IBOutlet weak var secondCollectionView: UICollectionView!
@IBOutlet weak var thirdCollectionView: UICollectionView!
我为他们设置了委托和数据源。但是当我想像下面这样设置numberOfItemsInSection时
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == firstCollectionView {
return 1
} else if collectionView == secondCollectionView {
return 2
} else if collectionView == thirdCollectionView {
return 3
}
}
它一直给我这个错误Missing return in a function expected to return 'Int'
【问题讨论】:
-
在函数末尾添加
return 0或将最后一个else if更改为else。您收到此错误是因为此函数不知道有 3 个 collectionView。所以如果没有一个if语句匹配,那么就没有返回 -
为了更好的可读性,我认为你最好在这里使用 switch 语句。您可以将缺少的
return 0放入default案例中。 -
它解决了@Scriptable的问题
-
它显示因为你没有返回 -> Int 为所需的功能,从 else if 中删除第三个集合视图条件并使其只有 else,错误将得到解决
标签: ios swift uicollectionview