简体   繁体   English

Swift Firebase将UID密钥与UID字符串数组匹配

[英]Swift Firebase match UID keys with a array of UID strings

I am trying to figure out how I can match the Strings which are Firebase UID's in my "data" array with the keys that I am extracting from the firebase call. 我试图弄清楚如何将“数据”数组中Firebase UID的字符串与从firebase调用中提取的密钥进行匹配。 I need to match the Strings in the "data" array with "key" and then I will be able to manipulate the data how I want. 我需要将“数据”数组中的字符串与“键”进行匹配,然后我将能够根据需要操纵数据。

 var data = [String]()

 Database.database().reference().child("Businesses").observe(.value, with: { snapshot in
        guard let allUsers = snapshot.children.allObjects as? [DataSnapshot] else {return}
        print(allUsers)
        print("Printing all users right here")
        for user in allUsers {
            let key = user.key
            print(key)
            print("Printing the keys right here come and check this out in the print")

        }
    })

在此处输入图片说明

在此处输入图片说明

My try, answer not in this code 我的尝试,不在此代码中回答

 Database.database().reference().child("Businesses").observeSingleEvent(of: .value, with: { snapshot in
            self.businessUID = snapshot.value as? NSDictionary
            if let dict = snapshot.value as? NSDictionary {
                for item in dict {
                    let json = JSON(item.value)
                    let businessUid = json["uid"].stringValue
                    for uid in self.data {
                        if uid == businessUid {
                            Database.database().reference().child("Businesses").observe(.value, with: { snapshot in
                                guard let allUsers = snapshot.children.allObjects as? [DataSnapshot] else {return}
                                print(allUsers)
                                print("Printing all users right here")
                                for user in allUsers {
                                    let key = user.key
                                    print(key)
                                    print("Printing the keys right here come and check this out in the print")
                                    if key == uid {
                                        print(key)
                                        print("printing the matching keys here")
                                    }

                                }
                            })
                            print(uid)
                            print("Printing the uids here")

                            Database.database().reference().child("Businesses").child(self.businessessuids).observe(.value, with: { snapshot in
                                print(snapshot)
                                print(self.businessessuids)

                            })

                        }
                    }
                }
            }

    })

UPDATE: Found matching values but cannot save 更新:找到匹配的值但无法保存

Database.database().reference().child("Businesses").observe(.value, with: { snapshot in
        if snapshot.exists() {
            for snapChild in snapshot.children {
                if let user: Dictionary? = (snapChild as! DataSnapshot).value as? [String : AnyObject] {
                    let businessUID = user!["uid"] as! String
                    for uid in self.data {
                        if uid == businessUID {
                            let key = uid.jsonKey
                            print(key)
                            //print(uid)
                            print("checking uid's")
                            //let name = user.childSnapshot(forPath: "name").value as! String
                            let Ref = Database.database().reference().child("testing")
                            let saveUID = Ref.child(key).child("their_name")
                        }
                    }
                }
            }
        }

    })

I've done some changes in snapshot. 我对快照做了一些更改。 Though I haven't verify as I don't have any Firebase database setup at my end as yours. 尽管我还没有验证,因为我像您一样没有任何Firebase数据库设置。

Database.database().reference().child("Businesses").observe(.value, with: { snapshot in

        if snapshot.exists() {

            for snapChild in snapshot.children {

                if let user: Dictionary? = (snapChild as! DataSnapshot).value as? [String : AnyObject] {

                    debugPrint("User uid: ", user!["uid"] as! String)
                }
            }
        }

    })

Replace your call with above code snippet and let me know if you are still having any issue. 将您的通话替换为上述代码段,如果您仍有任何问题,请告诉我。

Note: You can also improvisation you all user structure into model for better use. 注意:您也可以将所有用户结构即兴化为模型,以更好地使用。

This works. 这可行。 Great Success! 巨大的成功!

Database.database().reference().child("Businesses").observe(.value, with: { snapshot in
        if snapshot.exists() {
            for snapChild in snapshot.children {
                if let user: Dictionary? = (snapChild as! DataSnapshot).value as? [String : AnyObject] {
                    let businessUID = user!["uid"] as! String
                    for uid in self.data {
                        if uid == businessUID {
                            let key = uid.description
                            print(key)
                            print("checking uid's")
                            let Ref = Database.database().reference().child("testing")
                            let saveUID = Ref.child(key).child("tested")
                            saveUID.setValue("String")
                        }
                    }
                }
            }
        }

    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM