Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 迭代Firebase中的快照子级_Ios_Swift_Firebase_Nsenumerator - Fatal编程技术网

Ios 迭代Firebase中的快照子级

Ios 迭代Firebase中的快照子级,ios,swift,firebase,nsenumerator,Ios,Swift,Firebase,Nsenumerator,我有一个Firebase资源,它包含几个对象,我想使用Swift对它们进行迭代。 我期望的工作如下(根据Firebase文档) 因此,快速迭代Firebase返回的NSEnumerator对象似乎有问题 真的很欢迎帮助 如果我读对了,这就是你想要的: var ref = Firebase(url: MY_FIREBASE_URL) ref.observeSingleEvent(of: .value) { snapshot in print(snapshot.childrenCount)

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

因此,快速迭代Firebase返回的NSEnumerator对象似乎有问题

真的很欢迎帮助

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

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)     
    }
}
    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)
            }
        })
第一种方法要求
nsemulator
返回所有对象的数组,然后可以用通常的方式枚举这些对象。第二种方法从
nsemulator
一次获取一个对象,可能更有效

在这两种情况下,枚举的对象都是
FIRDataSnapshot
对象,因此需要强制转换,以便访问
属性


循环中使用

自从在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)     
    }
}

这本书可读性很好,效果也不错:

var ref = Firebase(url:MY_FIREBASE_URL)
ref.childByAppendingPath("some-child").observeSingleEventOfType(
  FEventType.Value, withBlock: { (snapshot) -> Void in

      for child in snapshot.children {

        let childSnapshot = snapshot.childSnapshotForPath(child.key)
        let someValue = childSnapshot.value["key"] as! String
      }
})

我刚刚将上述答案转换为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)
           }
})
更好的方法可能是:

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

Firebase4.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)
            }
       }
Firebase7.3.0

       Database.database().reference().child("key").observe(.value) { snapshot in
            if let datas = snapshot.children.allObjects as? [DataSnapshot] {
                let results = datas.compactMap({
                  ($0.value)
                })
                print(results)
            }
       }
如果您有多个键/值,并且希望使用
字典
元素
返回
数组
,请声明数组:

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

如果我只想从firebase数据库中删除该键的someValue,我该怎么办?对于SDK v2,我们是否必须在forloop中进行承诺回调确保我们在做其他事情之前完成了所有工作没有理由调用
childSnapshotForPath
,每个子项都已经是子项快照。谢谢,但
print(rest.value)
prints
Optional(0)
它应该打印一个整数。如何使
print(rest.value)
打印一个整数?