iOS error 'Invalid type in JSON write (FIRTimestamp)'

痴心易碎 提交于 2019-12-25 01:37:58

问题


I am trying to map my data to Model. Where I am using Firestore snapshot listener, to get data. here, I am getting data and mapping to "User" model that;

            do{
                let user = try User(dictionary: tempUserDic)
                print("\(user.firstName)")
            }
            catch{
                print("error occurred")
            }

Here is my Model:

struct User {
    let firstName: String
//    var lon: Double = 0.0
//    var refresh:Int = 0
//    var createdOn: Timestamp = Timestamp()
}

//Testing Codable
extension User: Codable {
    init(dictionary: [String: Any]) throws {
        self = try JSONDecoder().decode(User.self, from: JSONSerialization.data(withJSONObject: dictionary))
    }
    private enum CodingKeys: String, CodingKey {
        case firstName = "firstName"
    }
}

Correct me if I am wrong.

Crashing because I am getting "Timestamp" in data.

Data getting from listener :

User Dictionary:

[\"firstName\": Ruchira,
 \"lastInteraction\": FIRTimestamp: seconds=1576566738 nanoseconds=846000000>]"

How to map "Timestamp" to Model?

Tried "CodableFirstore" https://github.com/alickbass/CodableFirebase

来源:https://stackoverflow.com/questions/59390181/ios-error-invalid-type-in-json-write-firtimestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!