I have tried many ways, but I still can't convert the timestamp to date (year month day..), now I have this problem,
problem appear Value of type 'Int' has no member 'seconds'
let date = Date(timeIntervalSince1970: timestamp.seconds)
Here is the complete code
func getDatas(){
let firestoreDB = Firestore.firestore()
firestoreDB.collection("Post").order(by: "date", descending: true).addSnapshotListener { snapshot, error in
if error != nil {
print(error?.localizedDescription ?? "Error while getting data from server!" )
}else{
if snapshot?.isEmpty != true && snapshot != nil {
self.postArray.removeAll(keepingCapacity: false)
for document in snapshot!.documents{
if let imageURL = document.get("imageUrl") as? String{
if let caption = document.get("caption") as? String{
if let email = document.get("email") as? String{
if let timestamp = document.get("timestamp") as? Int{
let date = Date(timeIntervalSince1970: timestamp.seconds)
let post = Post(email: email, caption: caption, imageUrl: imageURL, timestamp: timestamp)
self.postArray.append(post)
}
}
}
}
}
self.tableView.reloadData()
}
}
}
CodePudding user response:
You have converted something from a network response into an Int
. You have then called <int>.seconds
. The error message is telling you that there is no such thing as .seconds
on an Int.
The timestamp that the Date
class is expecting is simply a Double
. You can use either the keyboard "Double" or "TimeInterval". Instead of casting as an Int, just do this:
if let timestamp = document.get("timestamp") as? TimeInterval {
let date = Date(timeIntervalSince1970: timestamp)
CodePudding user response:
Timestamp is just an int (number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z)).
So doesn't have a seconds property.
Have you tried:
let date = Date(timeIntervalSince1970: timestamp)
???