Swift firebase时间戳无法显示

时间:2018-05-14 09:30:57

标签: ios swift

我能够检索并转换我从firebase获得的时间戳(我在那条线上做了一个断点,我的时间显示 5/3 / 18,3:05 PM ,所以没关系),下面是我的代码:

    func loadMsg() {
    let toId = user!.id!
    let fromId = Auth.auth().currentUser!.uid
    let chatRoomId = (fromId < toId) ? fromId + "_" + toId : toId + "_" + fromId

    let ref = Database.database().reference().child("privateMessages").child(chatRoomId)
    ref.observe(.value) { (snapshot) in

        ref.observeSingleEvent(of: .childAdded, with: { (datasnap) in
            let lastMsgTime = (datasnap.value as! [String: AnyObject])["timestamp"] as? Double

            // to get timestamp and convert to date and time
            let x = lastMsgTime!
            let date = NSDate(timeIntervalSince1970: x)
            let formatter = DateFormatter()
            formatter.dateStyle = .short
            formatter.timeStyle = .short
            let time = formatter.string(from: date as Date)

            self.message.timestamp = time //HERE IT CRASHES!!!!!

        if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {

            self.messages.removeAll()

            for data in snapshot {

                let newMsg = Message(dictionary: data.value as! [String: AnyObject])

                self.messages.append(newMsg)

            }
        }
            })
        DispatchQueue.main.async {self.tableView.reloadData()}
    }
}

然而,它在我上面评论的那一行崩溃了,我应该在我的消息单元格中的tableviewCell上显示它。我的tableView单元格由xib完成,如下所示:

enter image description here

我的tableviewCell的代码是:

{

    self.message = message

    if message.fromId == currentUser {

        sentView.isHidden = false

        sentMsgLabel.text = message.textMessages

        receivedMsgLabel.text = ""

        receivedView.isHidden = true

        timeReceived.text = message.timestamp

        timeSent.text = message.timestamp

    } else {

        sentView.isHidden = true

        sentMsgLabel.text = ""

        receivedMsgLabel.text = message.textMessages

        receivedMsgLabel.isHidden = false

        timeReceived.text = message.timestamp

        timeSent.text = message.timestamp
    }
}

那么,为什么会崩溃并说

  

“线程1:致命错误:在展开时出乎意料地发现nil   可选值“

我的消息类代码:

    class Message: NSObject {

    var fromId: String?
    var textMessages: String?
    var timestamp: String?
    var toId: String?
    var message: Message!
    var _messageKey: String!


    init(dictionary: [String: Any]) {
        self.fromId = dictionary["fromId"] as? String
        self.textMessages = dictionary["textMessages"] as? String
        self.toId = dictionary["toId"] as? String
        self.timestamp = dictionary["timestamp"] as? String
    }

    init(messageKey: String, postData: Dictionary<String, AnyObject>) {

        _messageKey = messageKey

        if let message = postData["textMessages"] as? String {

            textMessages = message
        }

        if let sender = postData["fromId"] as? String {

            fromId = sender
        }
    }

}

我相信我的firebase没有任何问题,主要问题是在tableview单元格,为什么不显示?

1 个答案:

答案 0 :(得分:0)

尝试从服务器获取时间戳,如下所示:

ServerValue.timestamp()