![](/img/trans.png)
[英]Error while receiving image array from the Child's folder Firebase Swift
[英]Receiving a Firebase snapshot from a child with an array SWIFT
因此,我目前正在尝试从Firebase数据库中获取数据并将其设置为自己的变量,但是每个图表的子级都是特定的日期和时间(yy.mm.dd.hms)。 因此,我有一个数组,用于存储我需要的所有日期,但是在调用快照时无法引用它们。 我尝试了这两种方法,它们会引发错误“(child :)必须是非空字符串,并且不包含'。” '#''$''['或']''“
var postCollection = [170802120618, 170802101427] //yy.mm.dd.hh.mm.ss
ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath.row]).observe(.value, with: { (snapshot) in
for item in snapshot.children{
let snapshotValue = snapshot.value as? NSDictionary
let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
currentCell.nameLabel.text = firstNameSnap
}
})
和
var postCollection = [170802120618, 170802101427] //yy.mm.dd.hh.mm.ss
let selection = self.postCollection[indexPath.row]
ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath).observe(.value, with: { (snapshot) in
for item in snapshot.children{
let snapshotValue = snapshot.value as? NSDictionary
let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
currentCell.nameLabel.text = firstNameSnap
}
})
数据库图表大致如下:
FIR{
users{
uid{
username: UserName
Posts{
170802120618{
First Name: first
}
}
}
}
}
对。 您希望子键是一个自动生成的哈希值。 您可以使用childByAutoId()
创建它们。 另外,如果我是您,我只会将该日期存储为字符串,并根据需要解析它们。 以下是一个示例:
Posts {
-Kebfdajksthm {
first_name: "first",
post_date: "yymmddhhmmss"
}
}
尝试这个
var post = [String]()
ref.observe(.value, with: { (snapshot) in
for item in snapshot.children{
self.post.append((item as AnyObject).key)
}
})
然后打印“ post” ,您将得到[“ 170802120618”,“ 170802101427”]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.