2017-04-20 83 views
0

這裏是我的打印print(childSnap),並試圖解析as? [String : Any],但總是失敗的任何替代方案。firebase數組解析json總是失敗

Snap (-Ki9GkBvIMofJiIByVeI) { 
     location = Cupertino; 
     name = "find hotel "; 
     userCreatedId = ""; 
    } 

let ref = FIRDatabase.database().reference() 
     let allGroup = ref.child("all-group") 
     allGroup.observe(.childAdded, with: { (snapshot) in 
      for childSnap in snapshot.children{  
        print(childSnap) 

       if let _childSnap = childSnap as? [String : Any] { 
        //this code not execute.. 
        let group = Group(json: _childSnap) 
        groups.append(group) 
       } 
      } 

     }, withCancel: nil) 
    } 

回答

1

類型轉換childSnapFIRDataSnapshot,然後訪問它的value財產。

if let _childSnap = (childSnap as! FIRDataSnapshot).value as? [String : Any] { 
    let group = Group(json: _childSnap) 
    groups.append(group) 
}