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-如何循环使用snapshot.hasChild(“…”)_Ios_Swift_Loops_Firebase Realtime Database - Fatal编程技术网

Ios Firebase-如何循环使用snapshot.hasChild(“…”)

Ios Firebase-如何循环使用snapshot.hasChild(“…”),ios,swift,loops,firebase-realtime-database,Ios,Swift,Loops,Firebase Realtime Database,我的数据库如下所示 users | @--uidABC | |---phone:"+13125550690" | |---followers | |----uid123 | |---timeStamp:111222 | |----uid456 |

我的数据库如下所示

users
  |
  @--uidABC
       |
       |---phone:"+13125550690"
       |
       |---followers
              |
              |----uid123
              |      |---timeStamp:111222
              |
              |----uid456
              |      |---timeStamp:777999
我使用
.observeSingleEvent
检查
uidABC
是否存在,如果存在,我想检查该路径下是否有名为
追随者的
子项

我可以使用
snapshot.hasChild(“followers”)
查看它是否可用,以及它是否如何循环遍历snapshot.hasChild(“…”)下的所有子项

我使用下面的代码,但它在错误的
快照中循环。当它应该使用
DataSnapshot
下的
followers

let ref = Database...child("users").child(uidABC)
ref.observeSingleEvent(of: .value, with: { (snapshot) in

    if !snapshot.exists() {

        // this user doesn't exist
        return
    }

    if !snapshot.hasChild("followers") {

       // this user exists but has no followers
       return
    }

    // this user exists and has followers now loop through them
    for uid in snapshot.children {

        let snapshot = uid as! DataSnapshot

        if let dict = snapshot.value as? [String:Any] {
            let timeStamp = dict["timeStamp"] as? Double
            // eg. check for a specific timeStamp
         }
     }
})

使用
子项

// this user exists and has followers now loop through them
for uid in snapshot.childSnapshot("followers") {

    guard let snapshot = uid as! DataSnapshot else {
     // Followers error
    }

    if let dict = snapshot.value as? [String:Any] {
        let timeStamp = dict["timeStamp"] as? Double
        // eg. check for a specific timeStamp
     }
 }
您可以在此处找到更多信息: 我从这里得到了答案


谢谢,我试过了,但是得到了一个错误-类型为'DataSnapshot'的值没有成员'child';你是说“孩子”吗?
child
被重命名为
childSnapshot(forPath:)
谢谢你纠正我:)@LanceSamariaI我都不知道我在纠正你。我以为你正在关闭一个完全不同的快照子属性。谢谢你的帮助
let ref = Database...child("users").child(uidABC)
ref.observeSingleEvent(of: .value, with: { (snapshot) in

    if !snapshot.exists() {

        // this user doesn't exist
        return
    }

    if !snapshot.hasChild("followers") {

        // this user exists but has no followers
        return
    }

    // this user exists and has followers now loop through them
    let childSnapshot = snapshot.childSnapshot(forPath: "followers") {

    for child in childSnapshot.children {

        let snap = child as! DataSnapshot

        let uid = snap.key

        if let dict = snap.value as? [String: Any] {

            let timeStamp = dict["timeStamp"] as? Double ?? 0
            // eg. check for a specific timeStamp
        }
    }
})