swift - 使用 Swift 3 在 Firebase 数据库中检索 Int

标签 swift firebase firebase-realtime-database

我有这个代码:

    userRef.observeSingleEvent(of: .value, with: { (snapshot) -> Void in

        print(snapshot)

        if let dict = snapshot.value as? [Int : Bool] {
            print(dict)
        }
    })

打印此快照:

Snap (availability) {
    1 = 1;
    2 = 1;
    3 = 1;
    4 = 1;
    5 = 1;
    6 = 0;
    7 = 0;
}

我的问题是我的 Dictionnary dict 没有初始化,因为 Int 值为 1,2,...,6,7 ; Firebase 可能不会将它们作为 Int 提供。我还尝试使用 dict 作为 [String : Bool] 检索这些值。也不工作。 [NSNumber : Bool]

同样的问题

我的问题:考虑到这些值的子名称是 Int,我如何检索这些值?

最佳答案

问题在于,当您以顺序方式使用 Int 时,Firebase 会将它们视为数组。 您应该避免像键一样使用 Int,因为:

if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array.

给你more details about the issue ! 还有一些best practices来自 Firebase 博客。

关于swift - 使用 Swift 3 在 Firebase 数据库中检索 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42600385/

相关文章:

Firebase:云功能,如何缓存 Firestore 文档快照

Firebase 函数 v 1.0 : Alternative of DeltaSnapshot. 在 v 1.0 后更改了()

ios - 如何在 Realm 中使用 < 或 <= 运算符搜索对象?

ios - 如何创建具有复杂内容的 UITableViewCells 以保持滚动流畅

swift - 了解快速自动完成中的返回类型

android - Firebase 将数据保存为列表,有时保存为 map 对象

ios - Swift 3 手势识别器 path.boundingBox 是无限的

cordova - 使用 Ionic 登录 Firebase Facebook

node.js - 从 Firebase 迁移到 NodeJS MongoDB

ios - Firebase使用Swift 3.0返回Optional()