【问题标题】:firebase array parse json always failedfirebase数组解析json总是失败
【发布时间】:2017-04-20 09:12:42
【问题描述】:

这是我打印的print(childSnap) 并尝试使用as? [String : Any] 进行解析,但总是失败任何替代。

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

【问题讨论】:

    标签: json swift firebase firebase-realtime-database


    【解决方案1】:

    childSnap 类型转换为FIRDataSnapshot,然后访问它的value 属性。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 2015-10-05
      • 1970-01-01
      • 2015-10-19
      相关资源
      最近更新 更多