xcode - 如何从 Firebase 在 Swift 中获取下一个数据

标签 xcode swift swift2 firebase xcode7

我想从当前数据中获取下一个 KEY。 所以我有:

{
    "Key1":"Value1",
    "Key2":"Value2",
    "Key3":"Value3"               
  }

我可以用这段代码得到它:

let historyUrl = firebase.childByAppendingPath("Data")

historyUrl.observeEventType(.Value, withBlock: {
            historyValue in 
            print(historyValue.value)
        })

当我不知道 KEY 的名称且不解码 json 时,如何在 Key1、Key2、Key 中获取单独的值?

最佳答案

您始终可以从快照中获取键和值

值(value):

print(historyValue.value)

对于键:

print(historyValue.key)

使用.value时,会读入父节点的全部内容。然后,您可以遍历快照以获取单个键:值对

for child in historyValue.children {
  print(child.key)
  print(child.value)
}

另一种选择是使用 .childAdded,它将一次遍历每个单独的 child ,然后在添加新 child 时再次迭代。

ref.observeEventType(.ChildAdded, withBlock: { historyValue in
    println(historyValue.key)
    println(historyValue.value)
})

关于xcode - 如何从 Firebase 在 Swift 中获取下一个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825147/

相关文章:

objective-c - UIImageJPEG表示

swift - 如何将物理体添加到 SKAtlasTexture 或通过 Images.xcassets 创建动画?

ios - iOS 中 "isOpaque"和 "isTranslucent"有什么区别?

ios - 使用 swift 1.2 之前版本构建框架

xcode - 如何在Xcode 9.3上将“快速打开”还原为黑暗主题?

xcode - 代码模式 : Loading TabBarController objects directly from independent . xib 文件(而不是来自 MainWindow.xib)

ios - Swift 如何更新(修改、删除、添加)JSON 条目

ios - 在 swift 中实现 GTScrollNavigationBar

ios - 将 Base64 字符串应用于 UIImageView 在 iOS Swift 2 中不起作用

ios - 如何在 UIImageView 中快速显示带有动画的图像