我已经环顾了一会儿,没有运气。我需要在firebase中获取某个字典键的字符串值。
如果您查看下面的图片,我需要在顶部设置该密钥,并且需要将其设置为等于字符串,我可以将其转移到其他视图控制器,以便当用户想要在共享者下发帖时,例如,我根据密钥进入正确的值,然后分享到我可以添加值的共享者。谢谢,我也不需要所有的值,只要我观察过单个事件,我需要获取每个页面或字典的密钥。
我的代码:
let ref = FIRDatabase.database().reference()
ref.child("Notes").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
if let pagers = snapshot.value as? [String : AnyObject] {
let numb = snapshot.key //what I want
for (_, val) in pagers {
if let useri = val["postUsername"] as? String {
if useri == FIRAuth.auth()?.currentUser?.uid {
let bindfl = Page()
if let title = val["title"] as? String, let descript = val["description"] as? String,
let sharers = val["sharers"] as? [String], let poster = val["postUsername"] as? String, let setter = val["setter"] as? String, let creatorName = val["creatorName"] as? String {
bindfl.title = title
bindfl.descriptions = descript
bindfl.setter = setter
bindfl.sharers = sharers
bindfl.usernameOfBinder = poster
bindfl.creatorName = creatorName
bindfl.theBit = numb
self.pages.append(bindfl)
答案 0 :(得分:1)
当你向数据库添加数据时,你可以这样做吗
let key = ref.("Notes").childByAutoId().key
let notes = ["userName": userName,
"description": description,
"title": title,
"author": author,
"keyID": key]
let childUpdates = ["/Notes/\(key)": notes]
ref.updateChildValues(childUpdates)
之后你就可以得到像这样的钥匙
bindfl.title = title
bindfl.descriptions = descriptions
bindfl.userName = userName
bindfl.author= author
bindfl.keyID= keyID
希望它有效。
答案 1 :(得分:0)
ref.child("Notes"). observeSingleEvent(of: .childAdded, with: {snapshot in
print("\(snapshot.key)")
})
将为您提供表格中每个条目的自动ID键
ref.child("Notes").queryOrdered(byChild: "creatorName").queryEqual(toValue: "posterName").observeSingleEvent(of: .childAdded, with: {snapshot in
var postID = snapshot.key
//update post different post using retrieved ID
let infoToAdd = ["newPostStuff" : true]
FIREBASE_REF!.child("Posts/\(postID)").updateChildValues(infoToAdd)
})
将为您提供给定创建者的每个帖子的自动ID,然后使用检索到的密钥更新不同表格上的值