我环顾了一段时间,没有运气。我需要在firebase中获取某个字典键的字符串值。
如果您看下面的图片,我需要在顶部放置该键,并且需要将其设置为等于字符串,这样我就可以与其他视图控制器进行联系,因此,例如当用户想要在共享者下发布帖子时,根据密钥确定正确的价值,然后分配给共享者,在那里我可以添加价值。谢谢,我也不需要所有值,仅当我观察到WithSingleEvent之后,就需要获取每个页面或字典的键。
我的代码:
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)
最佳答案
好吧,当您向数据库中添加数据时,您可以这样做
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
希望它能起作用。
关于ios - 如何检索某些childByAutoId()键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45404350/