从Firebase快照阵列中获取数据 [英] Get data out of array of Firebase snapshots

查看:140
本文介绍了从Firebase快照阵列中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firebase快照数组 var guardians:Array< FIRDataSnapshot> = [] 。我想使用这样的循环访问每个快照的子值:

  print(self.guardians)
快照self.guardians {
让guardianID =(snapshot.value as?NSDictionary)?[guardian] as? NSString

但是我始终得到了guardianID的nil。我怎样才能从这个数组中获得特定的子值?作为参考,这里是self.guardians打印到控制台:

  [Snap(guardians){
-KQaU9lVcUYzIo52LgmN = {
dependent = sJUCytVIWmX7CgmrypqNai8vGBg2;
guardian = jY2MtbIw4rQmgcZwTbLXNrL7tDq2;
};
}]

我使用Swift 3和Xcode 8。 b
$ b

谢谢!

解决方案

我不确定如果不知道该数组已建成。一个问题是For-in循环变量是一个NSFastEnumerationIterator,所以它需要被降级到你想要的东西。你可能已经这样做了,所以这只是为了将来的参考。



这就是说,我制定了一些代码,并分解了一下 - 也许它会指向你正确的方向。

以下代码通过.value读取所有节点,然后遍历快照并将每个子节点存储在一个数组中作为FDataSnapshot。然后代码遍历这个数组,并从存储在数组中的每一个快照中打印guardian。

我还包含了你的代码来打印guardianID,它也能工作所以我的猜测是问题在于数组如何被初始填充或在这一行


用于self.guardians中的快照


作为快照是NSFastEnumerationIterator

 让ref = self.myRootRef.child(byAppendingPath:guardian_node)! 
$ b ref.observe(.value,其中:$快照
$ b $如果(snapshot!.value是NSNull){
print(not found)
$ else $ {

var guardianArray = [FDataSnapshot]()

快照中的子节点!.children {
let snap = child as!FDataSnapshot / / downcast
guardianArray.append(snap)
}

for guardianArray {
let dict = child.value as!NSDictionary
let thisGuardian = dict [guardian]!
print(thisGuardian)

// your code
let guardianID =(child.value as?NSDictionary)?[guardian] as? NSString
print(guardianID!)
}
}
})

*这是Swift 3和Firebase 2.x,但概念是相同的。


I have an array of Firebase snapshots var guardians: Array<FIRDataSnapshot> = []. I'd like to access each snapshot's child value using a loop like this:

 print(self.guardians)
 for snapshot in self.guardians{
     let guardianID = (snapshot.value as? NSDictionary)?["guardian"] as? NSString

But I keep getting nil for guardianID. How can I get specific child values out of this array? For reference, here is what self.guardians prints to console:

[Snap (guardians) {
    "-KQaU9lVcUYzIo52LgmN" =     {
        dependent = sJUCytVIWmX7CgmrypqNai8vGBg2;
        guardian = jY2MtbIw4rQmgcZwTbLXNrL7tDq2;
    };
}]

I'm using Swift 3 with Xcode 8.

Thanks!

解决方案

I am not sure if the question can be answered without knowing how the array is built. One gotcha is the For-in loops variable is a NSFastEnumerationIterator so it needs to be downcast to what kind of 'thing' you want it to be. You may have done that so this is just for future reference.

That being said, I crafted up some code and broke it down a bit - maybe it will point you in the right direction.

The code below reads in all nodes by .value and then iterates over the snapshot and stores each child in an array as a FDataSnapshot. Then the code iterates over that array and prints the 'guardian' from each snapshot stored in the array.

I also included your code to print the guardianID and it works as well so my guess is the problem lies with how the array is being initially populated or in this line

for snapshot in self.guardians

as the snapshot is a NSFastEnumerationIterator

let ref = self.myRootRef.child(byAppendingPath: "guardian_node")!

ref.observe(.value, with: { snapshot in

     if ( snapshot!.value is NSNull ) {
          print("not found")
     } else {

          var guardianArray = [FDataSnapshot]()

          for child in snapshot!.children {
               let snap = child as! FDataSnapshot //downcast
               guardianArray.append(snap)
          }

          for child in guardianArray {
               let dict = child.value as! NSDictionary
               let thisGuardian = dict["guardian"]!
               print(thisGuardian)

               //your code
               let guardianID = (child.value as? NSDictionary)?["guardian"] as? NSString
               print(guardianID!)
          }
     }
})

*This is Swift 3 and Firebase 2.x but the concept is the same.

这篇关于从Firebase快照阵列中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆