skip to Main Content

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’
(已解決)


Here is my edited code, but the time is showing on Xcode instead of the simulator
如圖片中顯示

code: VC

 if let timestamp = document.get("timestamp") as? TimeInterval {
     let date = Date(timeIntervalSince1970: timestamp)
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"
     let today = dateFormatter.string(from: date)
     print("Time Stamp's Current Time:(today)")
     let post = Post(email: email, caption: caption, imageUrl: imageURL, timestamp: timestamp)
...
...
...
...
cell.timeLabel.text = String(postArray[indexPath.row].timestamp)

code: Post

 struct Post {
    var email:String
    var caption:String
    var imageUrl:String
    var timestamp:Double

    init(email:String,caption:String,imageUrl:String,timestamp:Double) {
        self.email = email
        self.caption = caption
        self.imageUrl = imageUrl
        self.timestamp = timestamp
    }
}

2

Answers


  1. 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)
    
    Login or Signup to reply.
  2. 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) ???

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search