【问题标题】:How to consume Rest API Firebase data in Swift 3?如何在 Swift 3 中使用 Rest API Firebase 数据?
【发布时间】:2017-04-18 01:56:03
【问题描述】:

我在 firebase 中有一个数据库,我想使用这些数据,并将其保存到我的对象的变量中,但是我不明白如何执行此操作。我已阅读 Firebase 文档,但没有。

到目前为止,我只有这个

let guia = firebase.child("guias_administracao")
guia.observe(FIRDataEventType.value, with: { (snapshot) in

print(snapshot.value as Any)
for item in snapshot.children{
    let g = GuiaAdmModel(
        link: (item as! String),
        nome: (item as! String),
        videoLink: (item as! String)
    )
    self.listGuiaAdm.append(g)
}

当我运行时,这个错误返回给我

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
Optional(<__NSArrayM 0x600000246db0>(
<null>,
{
    link = "guia_administracao/MERCK_GUIA_UTILIZACAO_DIGITAL_fertilidade_2016_09_22_Cetrotide.pdf";
    nome = Cetrotide;
    videoLink = "//player.vimeo.com/video/183506335?color=ff0179&byline=0&portrait=0";
},
{
    link = "guia_administracao/MERCK_GUIA_UTILIZACAO_DIGITAL_fertilidade_2016_09_22_Crinone.pdf";
    nome = Crinone;
    videoLink = "";
},
{
    link = "guia_administracao/MERCK_GUIA_UTILIZACAO_DIGITAL_fertilidade_2016_09_22_Gonalf.pdf";
    nome = Gonalf;
    videoLink = "";
},
{
    link = "gs://fertility-b4db3.appspot.com/guia_administracao/Ovidrel.pdf";
    nome = Ovidrel;
    videoLink = "";
}
)
)
Could not cast value of type 'FIRDataSnapshot' (0x1061f7260) to 'NSString' (0x1079fdc60).

请帮帮我 一些示例或教程会很有帮助

【问题讨论】:

    标签: ios rest firebase swift3 firebase-realtime-database


    【解决方案1】:

    假设您的模型设置正确,以下内容应该对您有用,如果没有,请将 init 更改为接受 snap.key 作为键,并将 snap.value 作为第二个参数(作为字典)。

    guia.observe(.value, with: { (snapshot) in
    if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
        for snap in snapshot {
            if let gDict = snap.value as ? Dictionary < String, AnyObject > {
                    let g = GuiAdmModel(
                        link: gDict["link"],
                        nome: gDict["nome"],
                        videoLink: gDict["videoLink"]
                    )
                    self.listGuiaAdm.append(g)
            }
        }
    }
    })
    

    【讨论】:

    • 非常感谢,我只需要进行 xcode 所暗示的小改动,就是这样...for snap in snapshot { if let gDict = snap.value as? Dictionary &lt; String, AnyObject &gt; { let g = GuiaAdmModel( link: gDict["link"] as! String, nome: gDict["nome"] as! String, videoLink: gDict["videoLink"] as! String ) self.listGuiaAdm.append(g) self.tableGuiaAdm.reloadData() } }
    • 很高兴为@Ghost 服务。如果您发现我的建议有帮助,请考虑将其标记为答案。我在这里比较新,声望点很重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多