gpt4 book ai didi

ios - Swift Firebase 将 UID 键与 UID 字符串数组匹配

转载 作者:行者123 更新时间:2023-11-28 10:32:51 27 4
gpt4 key购买 nike

我想弄清楚如何将我的“数据”数组中的 Firebase UID 字符串与我从 firebase 调用中提取的键相匹配。我需要将“数据”数组中的字符串与“键”相匹配,然后我就可以按我想要的方式操作数据。

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

}
})

enter image description here

enter image description here

我的尝试,答案不在此代码中

 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)

})

}
}
}
}

})

更新:找到匹配值但无法保存

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

})

最佳答案

我对快照做了一些修改。虽然我还没有验证,因为我没有像你一样设置任何 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)
}
}
}

})

用上面的代码片段替换你的电话,如果你仍然有任何问题,请告诉我。

注意:您还可以将所有用户结构即兴创作到模型中以便更好地使用。

关于ios - Swift Firebase 将 UID 键与 UID 字符串数组匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55293180/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com