【问题标题】:Retrieve Dictionary From Firebase Swift从 Firebase Swift 中检索字典
【发布时间】:2016-08-19 08:27:42
【问题描述】:

我试图用不同的标签对字典的每个项目进行排序(在我的例子中: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
    })

}

任何线索都会非常有用!

感谢您的宝贵时间!

FireBase

【问题讨论】:

    标签: ios swift dictionary firebase


    【解决方案1】:

    鉴于您的 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 中的帖子中检索特定的子数据。

    假设我们有一个表格视图来列出我们的帖子。每个帖子的键(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
    }
    

    【讨论】:

    • 感谢您的回答,我可以这样做,但我只想访问一个特定的节点(根据所选节点的 nodeID 进行更改),并且我希望在该节点中能够检索不同的孩子。是否可以获得一个变量来根据所选帖子更改密钥?为了使我能够检索路径还是应该以其他方式进行?
    • @PierreDepingon 我不确定我是否完全理解这个问题。我想你要问的是;如果用户选择了一个帖子(在 ui 中),您想从 Firebase 中检索该帖子的子数据?
    • 对不起,没错!
    • @PierreDepingon 我在答案中添加了其他内容。希望对您有所帮助。
    猜你喜欢
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多