gpt4 book ai didi

ios - 从 Firebase Swift 检索字典

转载 作者:可可西里 更新时间:2023-11-01 01:36:31 25 4
gpt4 key购买 nike

我试图在不同的标签中对字典的每个项目进行排序(在我的例子中:在 firebase 中投票指定“votelabel”,在“textlabel”中输入文本......),我试图这样做但我真的有货。我认为我的问题是因为每个帖子都有一个新 key 而且我不知道如何直接转到 children

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
})

}

任何线索都会非常有用!

感谢您的宝贵时间!

FireBase

最佳答案

鉴于您的 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

根据更多信息,问题是:

How do I retrieve specific child data from a post in Firebase.

假设我们有一个列出我们帖子的表格 View 。每个帖子的键(node_0、node_1 和 node_2)应该存储在与 tableView 匹配的数组中(它可以用作数据源)

当用户单击或点击一行时,例如第 1 行,在数组中查找数据。在这种情况下,假设他们点击 tableView 中的第 1 行:

var theKey = myArray[1]//返回名为“node_1”的键

现在我们有了 key ,从 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
}

关于ios - 从 Firebase Swift 检索字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36849187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com