使用Swift 3

时间:2017-03-04 19:29:38

标签: 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]

相同的问题

我的问题:如果考虑到孩子的名字是Ints,我该如何检索这些值呢?

1 个答案:

答案 0 :(得分:1)

问题在于,当您按顺序使用Ints时,Firebase会将它们视为数组。 你应该避免使用像键这样的Int,因为:

  

如果所有键都是整数,并且对象中超过一半的键与对象中的最大键具有非空值,则Firebase会将其渲染为数组。

在这里more details about the issue! 还有来自Firebase博客的一些best practices