在Firebase中迭代快照子项

时间:2014-12-07 10:54:26

标签: ios swift firebase nsenumerator

我有一个包含多个对象的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) // I got the expected number of items
    for rest in snapshot.children { //ERROR: "NSEnumerator" does not have a member named "Generator"
       println(rest.value)     
     }
 })

因此,似乎Swift迭代Firebase返回的NSEnumerator对象存在问题。

非常欢迎帮助。

5 个答案:

答案 0 :(得分:85)

如果我读了documentation权利,这就是你想要的:

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)     
    }
}

更好的方法可能是:

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)     
    }
}

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

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


使用for-in循环:

自从在Swift 1.2天内写回原始答案以来,该语言已经发展。现在可以使用for in循环直接与枚举器一起使用case let来分配类型:

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)     
    }
}

答案 1 :(得分:17)

我刚刚将上述答案转换为 Swift 3:

ref = FIRDatabase.database().reference()    
ref.observeSingleEvent(of: .value, with: { snapshot in
       print(snapshot.childrenCount) // I got the expected number of items
       for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
           print(rest.value)
           }
})

更好的方法可能是:

    ref = FIRDatabase.database().reference() 
    ref.observeSingleEvent(of: .value, with: { 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)
            }
        })

答案 2 :(得分:16)

这很可读,效果很好:

{{1}}

答案 3 :(得分:7)

   ref = FIRDatabase.database().reference().child("exampleUsernames")    
   ref.observeSingleEvent(of: .value, with: { snapshot in

       for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {

           guard let restDict = rest.value as? [String: Any] else { continue }
           let username = restDict["username"] as? String
       }
   })

答案 4 :(得分:2)

Firebase 4.0.1

       Database.database().reference().child("key").observe(.value) { snapshot in
            if let datas = snapshot.children.allObjects as? [DataSnapshot] {
                let results = datas.flatMap({
                  ($0.value as! [String: Any])["xxx"]
                }
                print(results)
            }
       }

如果您有多个键/值,并想要return arraydictionary元素,请声明一个数组:

var yourArray = [[String: Any]]()

然后将块体更改为:

     let children = snapshot.children
     while let rest = children.nextObject() as? DataSnapshot, let value = rest.value {
          self.yourArray.append(value as! [String: Any])
      }