Can Swift 4's JSONDecoder be used with Firebase Realtime Database?

后端 未结 7 1634
有刺的猬
有刺的猬 2020-12-28 19:39

I am trying to decode data from a Firebase DataSnapshot so that it can be decoded using JSONDecoder.

I can decode this data fine when I use a URL to access it with a

7条回答
  •  时光说笑
    2020-12-28 20:33

    You can use this library CodableFirebase or the following extension can be helpful.

    extension JSONDecoder {
    
    func decode(_ type: T.Type, from value: Any) throws -> T where T : Decodable {
        do {
            let data = try JSONSerialization.data(withJSONObject: value, options: .prettyPrinted)
            let decoded = try decode(type, from: data)
            return decoded
    
        } catch {
            throw error
        }
    }
    

提交回复
热议问题