func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch section {
case 0:
重新声明一个var语句
one = Database.database().reference()
从数据库中收集信息
one.child("users").child("profile").observe(.value, with: {(snapshot) in
let num = snapshot.childrenCount
错误发生的地方
return Int(num)
})
default:
return 0
}
}
最佳答案
你传递给 observe 的这个函数被类型化为一个返回 void 的函数:
{(snapshot) in
let num = snapshot.childrenCount
return Int(num)
})
您可以从 API 文档中看到 observe :
Declaration
func observe(_ eventType: DataEventType, with block: @escaping (DataSnapshot) -> Void) -> UInt
函数的类型是(DataSnapshot) -> Void
,也就是说它不能返回值。但是您返回的是 Int(num)
。
您将无法从 collectionView() 返回数据库查询的结果,因为数据库观察器是异步的并且会立即返回。它会在一段时间后给您一个值,您无法保证需要多长时间。
关于swift - 我在 void 函数错误中有一个意外的非 void 返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53918927/