2016-10-04 62 views
2

swiftyJSON对于解析Firebase看起来很不错。什么愿与落得为2点的可能性:Firebase和swiftyJSON解析

imageName一个单独的ID,并为所有的ID的imageName的List。 JSON Struture:

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

我的最终结果是具有IMAGEURL中的CollectionView显示落得:检索JSON很好,但不显示imageName

enter image description here

代码。它似乎很接近,只是没有得到正确的swiftyJSON格式。谢谢。

回答

3

首先我想你应该解析你的数据'firebase的方式'我想你可以调用它而不是使用SwiftJSON。这是我会怎么做它的“火力方式”

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

    } 
} 

上面的代码很适合我

在SwiftJSON,我不是100%肯定这是否会工作,但也许我们可以尝试

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