草庐IT

ios - 从 Firebase Swift 检索字典

coder 2023-09-15 原文

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

有关ios - 从 Firebase Swift 检索字典的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. 微信小程序通过字典表匹配对应数据 - 2

    前言一般来说,前端根据后台返回code码展示对应内容只需要在前台判断code值展示对应的内容即可,但要是匹配的code码比较多或者多个页面用到时,为了便于后期维护,后台就会使用字典表让前端匹配,下面我将在微信小程序中通过wxs的方法实现这个操作。为什么要使用wxs?{{method(a,b)}}可以看到,上述代码是一个调用方法传值的操作,在vue中很常见,多用于数据之间的转换,但由于微信小程序诸多限制的原因,你并不能优雅的这样操作,可能有人会说,为什么不用if判断实现呢?但是if判断的局限性在于如果存在数据量过大时,大量重复性操作和if判断会让你的代码显得异常冗余。wxswxs相当于是一个独立

  5. ruby-on-rails - 使用 HTTP.get_response 检索 Facebook 访问 token 时出现 Rails EOF 错误 - 2

    我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token

  6. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  7. python - 将 Ruby 哈希字符串转换为 Python 字典 - 2

    我正在处理一些作为Ruby哈希字符串返回的命令输出。(来自名为mcollective的东西)。这是我收到的示例字符串:{:changes=>{"total"=>0},:events=>{"failure"=>0,"success"=>0,"total"=>0},:version=>{"puppet"=>"2.7.21(PuppetEnterprise2.8.1)","config"=>1381497648},:time=>{"filebucket"=>0.000287,"cron"=>0.00212,"package"=>0.398982,"exec"=>0.001314,"confi

  8. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

  9. ruby - IO::EAGAINWaitReadable:资源暂时不可用 - 读取会阻塞 - 2

    当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab

  10. ruby-on-rails - 如何检索网站图标? - 2

    我正在使用RubyonRailsv3.0.9,我想检索我设置了链接的每个网站的favicon.ico图像。也就是说,如果在我的应用程序中我设置了http://www.facebook.com/URL,我想检索Facebook的图标并在我的网页中使用\插入它。当然,我也想为所有其他网站这样做。如何以“自动”方式从网站检索favicon.ico图标(“自动”是指在网站中搜索图标并获取它的链接-我认为不是,因为并非所有网站都有一个名为“favicon.ico”的图标。我想以“自动”方式识别它)?P.S.:我想做的是像Facebook在您的Facebook页面中添加链接\URL时所做的那样:它

随机推荐