我正在尝试在不同的标签中对字典中的每个项目进行排序(在我的例子中:在firebase中输入指定的“votelabel”,“textlabel”中的文本...),我试着这样做但我真的得到了股票。我认为我的问题是因为每个帖子都有一个新密钥,我不知道如何直接去孩子们
var posts: [String: AnyObject] = [String: AnyObject]()
@IBAction func save(sender: AnyObject) {
let titre = Titre.text
let soustitre = SousTitre.text
let text = Text.text
let newPosts: Dictionary<String, AnyObject> = [
"titre": titre!,
"soustitre": soustitre!,
"text": text!,
"votes": votes
]
ref.childByAppendingPath(current).childByAppendingPath("Posts").childByAutoId().setValue(newPosts)
ref.childByAppendingPath(current).observeEventType(.ChildAdded, withBlock: { snapshot in
print(snapshot.value)
从这里我真的迷路了,我在教程中找到了这个部分,但是我认为我没有得到它。
var posts = [NSDictionary]()
for item in snapshot.children{
let child = item as! FDataSnapshot
let dict = child.value as! NSDictionary
posts.append(dict)
}
self.TextLabel.text = snapshot
})
}
任何线索都会非常有用!
谢谢你的时间!答案 0 :(得分:2)
鉴于您的Firebase结构如下所示
posts
node_0
title: "some title"
text: "some text"
vote: "some vote"
node_1
title: "another title"
text: "another text"
vote: "another vote"
node_2
title: "yet another title"
text: "yet another text"
vote: "yet another vote"
和一些代码来读取所有帖子节点并显示他们的孩子。快照返回一组键:值对,因此使用键访问值
let myRootRef = Firebase(url:"https://jaytest.firebaseio.com")
let postsRef = myRootRef.childByAppendingPath("posts"
postsRef.observeSingleEventOfType(.Value, withBlock { snapshot in
for child in snapshot.children {
if let title = child.value["title"] as? String {
print(title)
}
if let text = child.value["text"] as? String {
print(text)
}
if let title = child.value["vote"] as? String {
print(vote)
}
}
}
输出
some title
some text
some vote
another title
another text
another vote
yet another title
yet another text
yet another vote
基于更多信息,问题是:
如何从Firebase中的帖子中检索特定子数据。
假设我们有一个列出我们帖子的tableview。每个帖子的密钥(node_0,node_1和node_2)应存储在与tableView匹配的数组中(它可以用作数据源)
当用户点击或点击一行时,例如第1行,查找数组中的数据。在这种情况下,假设他们点击tableView中的第1行:
var theKey = myArray [1] //返回一个名为'node_1'的键
现在我们有了密钥,从Firebase获取数据是一个“快照”
postsRef = rootRef.childByAppendingPath("posts")
thisPostRef = postsRef.childByAppendingPath(theKey)
thisPostRef.observeSingleEventOfType(.Value withBlock { snapshot in
let title = snapshot.value["title"] //title = another title
let text = snapshot.value["text"] // text = another text
let vote = snapshot.value["vote"] //vote = another vote
}