swift - 我在 void 函数错误中有一个意外的非 void 返回值?

标签 swift firebase firebase-realtime-database switch-statement

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/

相关文章:

ios - 如何选择合适的 UIGestureRecognizer?

arrays - 如何在更大的数组中显示每个数组的值?

ios - 使用 pod Google/Cloud Messaging 时 iTunes Store 应用程序验证错误

c# - 允许 Firebase Xamarin 实时流式传输

android - 如何为 firebase 数据库子项声明字符串值

swift - 快速从 firebase 数据库中读取子值

ios - 更新 UITableView 内的 UIView 帧大小 - swift

Firebase 身份验证类型错误 : Cannot read property 'getIdToken' of null

swift - 使用不同的 View Controller 删除 map 上的数据

ios - 如何在 iOS objective-c 中将 firebase 时间戳转换为日期?