我有这个代码:
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/