2016-10-04 66 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)") 
}