首页 > 解决方案 > 从 Swift 中的 firebase 获取时间戳

问题描述

我想从 firebase 获取我的时间戳并将其转换为我们的正常时间 hh:mm: a 并且我已经完成了:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: PrivateMsgMainMenuTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PrivateMsgMainMenuTableViewCell

        let ref = Database.database().reference().child("privateMessages")

        ref.observeSingleEvent(of: .childAdded) { (snapshot) in
            for rest in snapshot.children.allObjects as! [DataSnapshot] {

                let messageID = rest.key

                let ref2 = Database.database().reference().child("privateMessages").child(messageID).queryLimited(toLast: 1)
                ref2.observeSingleEvent(of: DataEventType.value) { (snap) in
                    let messageDetails = snap.children

                    while let timeSent = messageDetails.nextObject() as? DataSnapshot {
                        let timeSentDetails = (timeSent.value as! [String: AnyObject])["timestamp"] as? Double

                        let converted = NSDate(timeIntervalSince1970: timeSentDetails! / 1000)

                        let dateFormatter = DateFormatter()
                        dateFormatter.timeZone = NSTimeZone.local
                        dateFormatter.dateFormat = "hh:mm a"
                        let time = dateFormatter.string(from: converted as Date)

                        DispatchQueue.main.async {
                            cell.timeLabel.text = time
                        }
                    }

                }
            }
    }
    return cell
}

我的火力基地看起来像: 在此处输入图像描述

我正在尝试检索最后发送消息的时间并显示在我的表格视图中的标签中,但我不确定为什么它不显示以及之后的代码

let timeSentDetails = (timeSent.value as! [String: AnyObject])["timestamp"] as? Double

当我设置断点时不运行。我在这里犯了什么错误吗?

标签: iosswiftfirebasefirebase-realtime-database

解决方案


推荐阅读