swift语言中云Firestore服务器时间戳的等效数据类型是什么?

时间:2018-05-08 15:32:38

标签: swift firebase timestamp google-cloud-firestore

我有一个包含时间戳字段createdAt的数据模型。

struct Box {
    var title: String
    var createdAt: Timestamp

    var dictionary: [String : Any] {
        return [
            "title": title,
            "createdAt": createdAt
        ]
    }

// ...

但是,我无法分配Firestore服务器时间戳。 swift中云Firestore服务器时间戳的等效数据类型是什么?我在这里错过了一点吗?

let box = Box(title: title, createdAt: FieldValue.serverTimestamp());

1 个答案:

答案 0 :(得分:7)

当您从Timestamp回读Firestore时,可以将其转换为Date对象:

if let timestamp = data["createdAt"] as? Timestamp {
    let date = timestamp.dateValue()
}
/** Returns a new NSDate corresponding to this timestamp. This may lose precision. */
- (NSDate *)dateValue;

Timestamp是一个FIRTimestamp对象,返回如下:

FIRTimestamp: seconds=1525802726 nanoseconds=169000000>

转换为.dateValue()后:

2018-05-08 18:05:26 +0000