我试图弄清楚如何将我的“数据”数组中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")
}
})
我的尝试,不在此代码中回答
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")
}
}
}
}
}
})
答案 0 :(得分:3)
我对快照做了一些更改。尽管我还没有验证,因为我像您一样没有任何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)
}
}
}
})
用上面的代码片段替换您的通话,如果您仍有任何问题,请告诉我。
注意:您也可以将所有用户结构即兴使用,以更好地使用。
答案 1 :(得分:0)
这有效。伟大的成功!
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")
}
}
}
}
}
})