创建项目时,我创建了一个Project类,并在timeStamp中为其指定了一个日期。
我无法将timeStamp
的项目对象的Date
转换为Decodable
我试图将timeStamp
返回到Double
,然后将其转换为Date
(timeIntervalSince1970: timestamp / 100)
我的班级:
class Project: Decodable, Identifiable{
var idP: String? = nil
var memberP: [String]
var nameP: String
var ratioP:Int
var startDateP:Date
var state: Int
init(idP:String, menberP:[String], nameP: String, ratioP: Int, timestamp:Double, state:Int) {
self.idP = idP
self.memberP = menberP
self.nameP = nameP
self.ratioP = ratioP
self.startDateP = Date(timeIntervalSince1970: timestamp / 100)
self.state = state
}
//扩展日期
extension Date
{
func calenderTimeSinceNow() -> String
{
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self, to: Date())
let years = components.year!
let months = components.month!
let days = components.day!
let hours = components.hour!
let minutes = components.minute!
let seconds = components.second!
if years > 0 {
return years == 1 ? "1 year ago" : "\(years) years ago"
} else if months > 0 {
return months == 1 ? "1 month ago" : "\(months) months ago"
} else if days >= 7 {
let weeks = days / 7
return weeks == 1 ? "1 week ago" : "\(weeks) weeks ago"
} else if days > 0 {
return days == 1 ? "1 day ago" : "\(days) days ago"
} else if hours > 0 {
return hours == 1 ? "1 hour ago" : "\(hours) hours ago"
} else if minutes > 0 {
return minutes == 1 ? "1 minute ago" : "\(minutes) minutes ago"
} else {
return seconds == 1 ? "1 second ago" : "\(seconds) seconds ago"
}
}
}
func read <T: Decodable>(from collectionReference: FIRCollectionReference, returning objectType: T.Type, completion: @escaping ([T]) -> Void)
{
reference(to: collectionReference).addSnapshotListener { (snapshot, _) in
guard let snapshot = snapshot else {return}
do {
var objetcs = [T]()
for document in snapshot.documents {
let object = try document.decode(as: objectType.self)
objetcs.append(object)
}
completion(objetcs)
} catch {
print(error)
}
}
}
```
the error in my console :
```
2019-05-09 23:12:07.302957+0200 GestionDeProjet[11755:366660] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (FIRTimestamp)'
*** First throw call stack:
```
Thanks to everyone who will read this post for your time:)