gpt4 book ai didi

swift - 如何在 ios 中将数据类型声明为时间戳 - swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:00:40 26 4
gpt4 key购买 nike

我是iOS编程新手。我正在创建一个与 Firestore 集成的简单程序。在 Firestore 中,我创建了集合,每个文档都包含许多字段。

我在文档中添加了 Timestamp 字段。当我在 xcode 中创建模型时,我如何将我的变量声明为 Timestamp 因为我需要根据 Timestamp 对 tableView 上的数据进行排序

这是 Firestore 中的 Timestamp 的样子:

2018 年 9 月 17 日下午 3:37:43 UTC+7

那么,我如何编写程序并获取当前的Timestamp,就像在Firestore中显示的那样

这是在编程中:

struct RecentCallModel {

var call_answered: Bool?
var call_extension: String?
var call_type: String?
var details: String?
var duration: String?
var title: String?
// How can i declare as Timestamp
var timestamp: ???

init(call_answered: Bool, call_extension: String, call_type: String, details: String , duration: String, timestamp: String, title: String) {

self.call_answered = call_answered
self.call_extension = call_extension
self.call_type = call_type
self.details = details
self.title = title
}


}

最佳答案

不熟悉 Firebase。但通常,在 iOS 中,我们将时间戳存储为 TimeInterval,它是 Double 的类型别名。

class func getDateOnly(fromTimeStamp timestamp: TimeInterval) -> String { 
let dayTimePeriodFormatter = DateFormatter()
dayTimePeriodFormatter.timeZone = TimeZone.current
dayTimePeriodFormatter.dateFormat = "MMMM dd, yyyy - h:mm:ss a z"
return dayTimePeriodFormatter.string(from: Date(timeIntervalSince1970: timestamp))
}

关于swift - 如何在 ios 中将数据类型声明为时间戳 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52367721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com