如何在 ios swift5 中编码 firebase 时间戳?
How to Codable firebase timestamp in ios swift5?
我正在尝试使用 FirebaseDecoder()
解码 firebase 集合。这是我使用的模型
方法一
struct firebaseApplication:Codable {
var uid:String?
var status:String?
var jobId:String?
var companyId:String?
var createdDate: Timestamp? // or // Date? // or // String?
}
如果我评论 createdDate 那么结果就是。否则没有结果。我尝试将 createdDate 作为 Timestamp?
和 Date?
和 string?
,所有情况都没有结果。
我尝试了另一种方式,它工作正常,这里是
方法二
struct ApplicationCollection:CustomStringConvertible {
var description: String
var uid: String
var status:String
var jobId:String
var companyId:String
var createdDate:Date
init(Dict:[String:Any]) {
self.uid = Dict["uid"] as? String ?? ""
self.status = Dict["status"] as? String ?? ""
self.jobId = Dict["jobId"] as? String ?? ""
self.companyId = Dict["companyId"] as? String ?? ""
let str = Dict["createdDate"] as? Timestamp ?? Timestamp()
self.createdDate = str.dateValue()
self.description = "[uid: \(uid),status: \(status),jobId: \(jobId),companyId: \(companyId),createdDate: \(createdDate)]"
}
init?(data: Data) {
guard let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else { return nil }
self.init(Dict: json)
}
init?(json: String) {
self.init(data: Data(json.utf8))
}
}
方法 2 可以正常获取所有数据以及 createDate
为什么在 方法 1 中没有得到 createDate?我错过了什么吗?
如何解决
看起来方法 2 有效,因为您手动将变量映射到 key/value 对。
方法 1 可能不起作用,因为您正在尝试将字符串映射到自定义类型(不确定 TimeStamp 是否可编码,因为未提供实现)而不是原始字符串。也许在方法一中将创建日期设置为字符串,并为该字符串的日期对象创建 accessor()。
方法 2 似乎你知道它是一个字符串,但是将 createdDate 设置为该时间戳的 dateValue
,因为类型是对齐的。
你能post你的 Timestamp 实施以供审查吗?
我一直在使用 https://github.com/alickbass/CodableFirebase 项目来为我节省大量关于此类内容的代码。它确实有助于处理 FireStore,它还可以处理时间戳。
我找到了问题的解决方案。
我错了,在解码Timestamp()
时我们必须使用FirestoreDecoder()
而不是FirebaseDecoder
。
这是我从 firebase 更改为 firestore
let decoder = FirebaseDecoder()
let data = try decoder.decode(firebaseApplicationCollection.self, from: i.data())
而不是
let decoder = FirestoreDecoder()
let item = try decoder.decode(firebaseApplicationCollection.self, from: i.data())
我希望这个答案能帮助其他人在解码 firebase
中的 Timestamp
时确定确切的问题
我正在尝试使用 FirebaseDecoder()
解码 firebase 集合。这是我使用的模型
方法一
struct firebaseApplication:Codable {
var uid:String?
var status:String?
var jobId:String?
var companyId:String?
var createdDate: Timestamp? // or // Date? // or // String?
}
如果我评论 createdDate 那么结果就是。否则没有结果。我尝试将 createdDate 作为 Timestamp?
和 Date?
和 string?
,所有情况都没有结果。
我尝试了另一种方式,它工作正常,这里是
方法二
struct ApplicationCollection:CustomStringConvertible {
var description: String
var uid: String
var status:String
var jobId:String
var companyId:String
var createdDate:Date
init(Dict:[String:Any]) {
self.uid = Dict["uid"] as? String ?? ""
self.status = Dict["status"] as? String ?? ""
self.jobId = Dict["jobId"] as? String ?? ""
self.companyId = Dict["companyId"] as? String ?? ""
let str = Dict["createdDate"] as? Timestamp ?? Timestamp()
self.createdDate = str.dateValue()
self.description = "[uid: \(uid),status: \(status),jobId: \(jobId),companyId: \(companyId),createdDate: \(createdDate)]"
}
init?(data: Data) {
guard let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else { return nil }
self.init(Dict: json)
}
init?(json: String) {
self.init(data: Data(json.utf8))
}
}
方法 2 可以正常获取所有数据以及 createDate
为什么在 方法 1 中没有得到 createDate?我错过了什么吗?
如何解决
看起来方法 2 有效,因为您手动将变量映射到 key/value 对。
方法 1 可能不起作用,因为您正在尝试将字符串映射到自定义类型(不确定 TimeStamp 是否可编码,因为未提供实现)而不是原始字符串。也许在方法一中将创建日期设置为字符串,并为该字符串的日期对象创建 accessor()。
方法 2 似乎你知道它是一个字符串,但是将 createdDate 设置为该时间戳的 dateValue
,因为类型是对齐的。
你能post你的 Timestamp 实施以供审查吗?
我一直在使用 https://github.com/alickbass/CodableFirebase 项目来为我节省大量关于此类内容的代码。它确实有助于处理 FireStore,它还可以处理时间戳。
我找到了问题的解决方案。
我错了,在解码Timestamp()
时我们必须使用FirestoreDecoder()
而不是FirebaseDecoder
。
这是我从 firebase 更改为 firestore
let decoder = FirebaseDecoder()
let data = try decoder.decode(firebaseApplicationCollection.self, from: i.data())
而不是
let decoder = FirestoreDecoder()
let item = try decoder.decode(firebaseApplicationCollection.self, from: i.data())
我希望这个答案能帮助其他人在解码 firebase
中的Timestamp
时确定确切的问题