使用firebase在swift3中的时间戳问题

时间:2017-04-19 08:02:41

标签: swift

当我添加新的反馈时,我发送时间戳,并在db.A中成功添加。当我点击服务后,我得到所有的反馈,包括新添加的带时间戳的反馈,当我解析所有这些数据然后新添加的反馈时间戳没有进入条件

if (dictionary["timestamp"] as? u_long) != nil {
        feedbackTimestamp = (dictionary["timestamp"] as? u_long)!
}
else{
        feedbackTimestamp = 0
}

它进入else并变为0但是所有其他反馈时间戳正在进入条件。如果有条件我退出应用程序并重新启动,那么新增加的反馈就更有用了。以下是我对服务器的回复。

(lldb) po print(dictionary)
["uniqueCode": IVIVC578OL5PH, "status": Pending Approval, 
"name": Test, "desc": Vjdjxjdbfcj, "id": feedback20170419_032044, 
"title": bidh, "timestamp": 1492586444448, "category": water, 
"uid": mGs1zKSblyLyZALNtKw8YiGEr962, 
"url": https://imageurl/o/images%2Ffeedback%2Ffeedback20170419_032041?alt=media&token=6f4e2d2b-2ced-4cf2-b8ad-07375fdae0d8]

2 个答案:

答案 0 :(得分:0)

尝试简单地投射到Int

使用nil-coalescing运算符可以将表达式简化为一行:

feedbackTimestamp = dictionary["timestamp"] as? Int ?? 0

如果feedbackTimestamp需要u_long

feedbackTimestamp = u_long(dictionary["timestamp"] as? Int ?? 0)

但我会使用更高级别的类型UInt而不是u_long

答案 1 :(得分:0)

尝试简单地投射到Double

feedbackTimestamp = dictionary["timestamp"] as? Double ?? 0