Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.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_Firebase Realtime Database - Fatal编程技术网

Ios 如何在子系统中从Firebase获取数据?

Ios 如何在子系统中从Firebase获取数据?,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我的数据结构如下所示: 通常情况下,其中应该有多个用户,因此我在频道ID中获得了更多的映射。我想从该频道ID中的第一个人处检索用户ID。在使用频道ID的正确路径时,我尝试了以下操作来检索用户ID,首先遵循正常代码: let firstChild = UInt(1) self.channelRef?.queryLimited(toFirst: firstChild).observeSingleEvent (of: .value, with: { (snapshot) in

我的数据结构如下所示:

通常情况下,其中应该有多个用户,因此我在频道ID中获得了更多的映射。我想从该频道ID中的第一个人处检索用户ID。在使用频道ID的正确路径时,我尝试了以下操作来检索用户ID,首先遵循正常代码:

let firstChild = UInt(1)

     self.channelRef?.queryLimited(toFirst: firstChild).observeSingleEvent
(of: .value, with: { (snapshot) in
                            let value = snapshot.value as? NSDictionary

    })
    if let newCreator = snapshot.childSnapshot(forPath: "userID").value
                            {
                                print(newCreator)
                            }

    let newCreator = value?.["userID"] as! String

    let newCreator = value?["userID"] as? String ?? ""

    if snapshot.key == "userID"
                {
                    let newCreator = snapshot.value as! String
                }
什么都不起作用,大多数情况下输出为零。这是快照的打印,然后是值的打印:

Snap (-KeLAUXw5juT1xBISOLA) {
    "-KeLCHhN3FPZ-chOE9MF" =     {
        PictureVersion = 2;
        readyToGo = 0;
        userID = SZlQ76RLCJQpFa0CDhrgFJoYzrs2;
        username = pietje;
    };
}
Optional({
    "-KeLCHhN3FPZ-chOE9MF" =     {
        PictureVersion = 2;
        readyToGo = 0;
        userID = SZlQ76RLCJQpFa0CDhrgFJoYzrs2;
        username = pietje;
    };
})
如何获取用户ID

编辑:似乎我得到的唯一一个密钥是来自用户的随机ID。我想我需要从更深一层获取数据,但如何做到这一点呢

let user = value.allValues.first! as! NSDictionary
let userID = user["userID"] as? String ?? ""
虽然当快照中没有用户时,这会崩溃,但请确保为此编写检查。这样做可能更好

self.channelRef?.queryLimited(toFirst: firstChild).observeSingleEvent(of: .value, with: { snapshot in
    let enumerator = snapshot.children
    while let user = enumerator.nextObject() as? FIRDataSnapshot {
        guard let userValue = user.value as? NSDictionary else {
            return
        }
        let userID = userValue["userID"] as? String ?? ""
    }
})