Firebase和swiftyJSON解析 [英] Firebase and swiftyJSON parsing

查看:129
本文介绍了Firebase和swiftyJSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

swiftyJSON对解析Firebase看起来不错。想要结束的是两种可能性:

一个个人ID的 imageName ,以及所有ID均为 imageName s。 JSON结构:





检索JSON的代码罚款,但不显示 imageName 值:

  ref.observe(FIRDataEventType .value){
(snapshot:FIRDataSnapshot!)in

let json = JSON(snapshot.value)
// print(JSON:\(json))

//打印IMAGENAME - 不工作
if imageName = json [12345] [0] [imageName]。string {
print(imageName:\( imageName))
}

// //打印IMAGENAME - 不工作
让theKeys = json.dictionary!.keys

theKeys {
let imageName = json [0] [0] [imageName]
print(imageName:\(imageName))
}
}

我的最终结果是在一个CollectionView中显示一个imageURL。看起来很接近,只是没有得到正确的swiftyJSON格式。首先,我想你应该解析你的数据的'firebase的方式'我想你可以调用它,而不是使用SwiftJSON 。这是我如何做'firebase的方式'

 导入FirebaseDatabase 

类ViewController:UIViewController {

var ref:FIRDatabaseReference!
$ b $重写fun viewDidLoad(){
super.viewDidLoad()

ref = FIRDatabase.database()。reference()



$ b $ func retrieveData(){

ref.child(12345)。observe(.childAdded,with {(snapshot)in

让dict = snapshot.value as?[String:AnyObject]

imageNamed = dict![imageName] as?String

print(\\ \\(imageNamed))
}

}
}

上面的代码适合我在SwiftJSON中使用

,我不是100%确定这是否可行,但也许我们可以试试$ / $>

 让json = JSON(snapshot.value)as?[String:AnyObject] 

if imageName = json! 12345] [0] [imageName]。string {

}

让theKeys = json.dictionary!.keys

在$ {$ b $ let imageName = json [0] [0] [imageName]
print(imageName:\(imageName))
}


swiftyJSON looks great for parsing Firebase. What would like to end up with is 2 possibilities:

An imageName for one individual ID, and a list of imageNames for all IDs. JSON Struture:

Code that retrieves JSON fine, but does not display imageName values:

ref.observe(FIRDataEventType.value) {
          (snapshot: FIRDataSnapshot!) in

            let json = JSON(snapshot.value)
            //  print("JSON:  \(json)")

            // PRINT IMAGENAME - NOT WORKING
            if let imageName = json[12345][0]["imageName"].string {
                print("imageName: \(imageName)")
            }

          // // PRINT IMAGENAME - NOT WORKING
          let theKeys = json.dictionary!.keys

          for key in theKeys {
            let imageName = json[0][0]["imageName"]
            print("imageName: \(imageName)")
          }
}

My end result is to end up with an imageURL to display in a CollectionView. It seems close, just not getting the swiftyJSON format correct. Thanks.

解决方案

First I think you should parse your data the 'firebase way' I guess you can call it instead of using SwiftJSON. This is how I would do it the 'firebase way'

    import FirebaseDatabase

    class ViewController: UIViewController {

        var ref: FIRDatabaseReference!

     override fun viewDidLoad() {
        super.viewDidLoad()

        ref = FIRDatabase.database().reference()


    }

    func retrieveData() {

        ref.child("12345").observe(.childAdded, with { (snapshot) in 

        let dict = snapshot.value as? [String: AnyObject]

        let imageNamed = dict!["imageName"] as? String

        print("\(imageNamed)")
    }

    }
}

The code above works well for me

in SwiftJSON, I'm not 100% sure if this will work but maybe we could try

let json = JSON(snapshot.value) as? [String: AnyObject]

if let imageName = json!["12345"][0]["imageName"].string {

}

let theKeys = json.dictionary!.keys

for key in theKeys {
    let imageName = json[0][0]["imageName"]
    print("imageName: \(imageName)")
}

这篇关于Firebase和swiftyJSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆