从Swift的firebase获取时间戳

时间:2018-05-08 06:49:40

标签: ios swift firebase firebase-realtime-database

我想从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
}

我的firebase看起来像: enter image description here

我正在尝试检索上次发送的消息的时间并显示在我的tableview中的标签中,但我不确定为什么它不显示并且后面的代码

let timeSentDetails = (timeSent.value as! [String: AnyObject])["timestamp"] as? Double
当我提出一个断点时,

不会运行。我在这里做过什么错误?

0 个答案:

没有答案