Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS错误';JSON写入(FIRTimestamp)和#x27中的类型无效;_Ios_Firebase_Swift4_Codable - Fatal编程技术网

iOS错误';JSON写入(FIRTimestamp)和#x27中的类型无效;

iOS错误';JSON写入(FIRTimestamp)和#x27中的类型无效;,ios,firebase,swift4,codable,Ios,Firebase,Swift4,Codable,我正在尝试将数据映射到模型。 我正在使用Firestore快照侦听器获取数据。 在这里,我正在获取数据并映射到“用户”模型 do{ let user = try User(dictionary: tempUserDic) print("\(user.firstName)") } catch{ print("error occurr

我正在尝试将数据映射到模型。 我正在使用Firestore快照侦听器获取数据。 在这里,我正在获取数据并映射到“用户”模型

            do{
                let user = try User(dictionary: tempUserDic)
                print("\(user.firstName)")
            }
            catch{
                print("error occurred")
            }
这是我的型号:

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"
    }
}
如果我错了,请纠正我

崩溃,因为我在数据中获取“时间戳”。

从侦听器获取的数据:

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"
    }
}
用户词典

[\"firstName\": Ruchira,
 \"lastInteraction\": FIRTimestamp: seconds=1576566738 nanoseconds=846000000>]"
如何将“时间戳”映射到模型?


尝试了“CodableFirstore”

代码没有写任何东西。要将数据映射到模型,必须使
时间戳
符合
可编码
<代码>对象映射器不是必需的。@瓦迪安,我没有使用“对象映射器”。当我试图在Codable中使用“Timestamp”时,它不允许我使用。使用
FIRTimestamp
的表示形式,它与
Codable
兼容,如
Date
。代码不写任何东西。要将数据映射到模型,必须使
时间戳
符合
可编码
<代码>对象映射器不是必需的。@瓦迪安,我没有使用“对象映射器”。当我试图在Codable中使用“Timestamp”时,它不允许我使用。使用
FIRTimestamp
的表示形式,它与
Codable
兼容,如
Date