使用Swift在Firebase中搜索钥匙来检索值



使用Swift在Firebase中搜索该键,如何获得键的值。说,拥有以下结构,我想通过搜索userId3来检索用户名3作为字符串?

users-used
    userID1 : userName1
    userID2 : userName2
    userID3 : userName3
    userID4 : userName4

假设'用户使用的'目录是结构的根源,您应该能够以下面的方式进行:

Database.database().reference().child("users-used").child("userID3").observeSingleEvent(of: .value)  { (snapshot) in
        guard let username = snapshot.value as? String else {
            return
        }
        // username will be available here as a string if successful
    }

请记住,封闭中的所有内容都将异步运行,并且可以随时返回

最新更新