gpt4 book ai didi

Swift Firebase ServerTime.timestamp() 返回负日期。

转载 作者:行者123 更新时间:2023-11-28 08:02:16 34 4
gpt4 key购买 nike

我的 Firebase 调用似乎返回了一个负值。我像这样存储日期值:

["ctn": ServerValue.timestamp()]

当我检查数据库时,它有:

 "users" {
"$uid" {
"ctn" : "1505930238317"
}
}

当我尝试将其从数据库中取出并像这样打印时:

dbRootRef.child("users").child(user.getUserUID()).child("ctn").observeSingleEvent(of: .value, with: { (snapshot) in

if let item = snapshot.value as? Int{
timeNow = item
print("db time \(item)")
}
}

控制台显示:

db time -1603282711

有什么想法吗?

最佳答案

时间戳是非常大的数字,在大多数类型化语言中,它们不符合整数数据类型的限制。

您得到一个负值,因为在将时间戳转换为整数时,您已将其设为 overflow .

使用DoubleUInt64 数据类型而不是Int,您应该会看到正确的值。

if let item = snapshot.value as? UInt64 {
timeNow = item // ensure that timeNow is declared as a UInt64
print("db time \(item)")
}

关于Swift Firebase ServerTime.timestamp() 返回负日期。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46328883/

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