在Firebase中迭代快照子项 [英] Iterate over snapshot children in Firebase

查看:145
本文介绍了在Firebase中迭代快照子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个对象的Firebase资源,我想使用Swift迭代它们。
我期望工作的内容如下(根据Firebase文档)

https://www.firebase.com/docs/ios-api/Classes/FDataSnapshot.html#//api/name/children

  var ref = Firebase(url:MY_FIREBASE_URL)
ref.observeSingleEventOfType(.Value,withBlock:{snapshot in
println(snapshot.childrenCount)//我在snapshot.children {$ ERROR:NSEnumerator没有名为Generator的成员的情况下得到预期的项目数
...
println (rest.value)
}
})

所以似乎有Swift迭代Firebase返回的NSEnumerator对象的问题。



非常欢迎帮助。

解决方案

如果我读了文档对,这就是你想要的:

  var ref = Firebase(url:MY_FIREBASE_URL)
ref.observeSingleEvent(of:。value){snapshot in
print(snapshot.childrenCount)//我得到了预期的项目数在snapshot.children.allObjects中休息
! [FIRDataSnapshot] {
print(rest.value)
}
}

更好的方法可能是:

  var ref = Firebase(网址:MY_FIREBASE_URL)
ref.observeSingleEvent( :.value){快照在
print(snapshot.childrenCount)//我得到了预期的项目数
let enumerator = snapshot.children
而let rest = enumerator.nextObject()如? FIRDataSnapshot {
print(rest.value)
}
}

第一种方法需要 NSEnumerator 返回所有对象的数组,然后可以通常的方式枚举这些对象。第二种方法从 NSEnumerator 一次获取一个对象,并且可能更有效。



在任何一种情况下,枚举的对象是 FIRDataSnapshot 对象,因此您需要强制转换,以便可以访问属性。 / p>




使用 for-in 循环:



由于在Swift 1.2天内写回原始答案,语言已经发展。现在可以在循环中使用来直接使用枚举器和 case let 来分配类型:

  var ref = Firebase(url:MY_FIREBASE_URL)
ref.observeSingleEvent(of:。value){snapshot in
print(snapshot.childrenCount)//我获得了预期的商品数量
for case let rest as FIRDataSnapshot in snapshot.children {
print(rest.value)
}
}


I have a Firebase resource that contains several objects and I would like to iterate over them using Swift. What I expected to work is the following (according to the Firebase documentation)
https://www.firebase.com/docs/ios-api/Classes/FDataSnapshot.html#//api/name/children

var ref = Firebase(url:MY_FIREBASE_URL)
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
  println(snapshot.childrenCount) // I got the expected number of items
    for rest in snapshot.children { //ERROR: "NSEnumerator" does not have a member named "Generator"
       println(rest.value)     
     }
 })

So it seems there is a problem with Swift iterating over the NSEnumerator object returned by Firebase.

Help is really welcome.

解决方案

If I read the documentation right, this is what you want:

var ref = Firebase(url: MY_FIREBASE_URL)
ref.observeSingleEvent(of: .value) { snapshot in
    print(snapshot.childrenCount) // I got the expected number of items
    for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
       print(rest.value)     
    }
}

A better way might be:

var ref = Firebase(url: MY_FIREBASE_URL)
ref.observeSingleEvent(of: .value) { snapshot in
    print(snapshot.childrenCount) // I got the expected number of items
    let enumerator = snapshot.children
    while let rest = enumerator.nextObject() as? FIRDataSnapshot {
       print(rest.value)     
    }
}

The first method requires the NSEnumerator to return an array of all of the objects which can then be enumerated in the usual way. The second method gets the objects one at a time from the NSEnumerator and is likely more efficient.

In either case, the objects being enumerated are FIRDataSnapshot objects, so you need the casts so that you can access the value property.


Using for-in loop:

Since writing the original answer back in Swift 1.2 days, the language has evolved. It is now possible to use a for in loop which works directly with enumerators along with case let to assign the type:

var ref = Firebase(url: MY_FIREBASE_URL)
ref.observeSingleEvent(of: .value) { snapshot in
    print(snapshot.childrenCount) // I got the expected number of items
    for case let rest as FIRDataSnapshot in snapshot.children {
       print(rest.value)     
    }
}

这篇关于在Firebase中迭代快照子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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