JSQMessagesViewController 7.3.4 + Firebase(3.7.1)observeMessages无法访问FIRDataSnapshot

时间:2016-10-07 15:53:23

标签: ios firebase swift3 jsqmessagesviewcontroller

我只是想用最新版本和Swift 3.0观察JSQMessages,以下代码是关于此问题的任何其他问题中报告但由于某种原因FIRDataSnapshot中包含的字典返回编译错误“Type'Any'没有下标成员。

enter image description here

有没有人遇到同样的问题并克服它?谢谢!

2 个答案:

答案 0 :(得分:1)

我更喜欢尽可能长时间地坚持使用Firebase数据类型。在这种情况下,您将获得以下值:

snapshot.childSnapshot(forPath: "text").value as! String

所以:

private func observeMessages() {
    let messagesQuery = messageRef.queryLimited(toLast: 25)

    messagesQuery.observe(.childAdded) { (snapshot: FIRDataSnapshot!) in
        let id = snapshot.childSnapshot(forPath: "name").value as! String
        let text = snapshot.childSnapshot(forPath: "text").value as! String
        self.addMessage(id: id, text: text)
        self.finishReceivingMessage()
    }
}

答案 1 :(得分:0)

我找到了一个解决方案,不确定它是最好的方法,基本上我强制转换为NSDictionary:

private func observeMessages() {
        let messagesQuery = messageRef.queryLimited(toLast: 25)

        messagesQuery.observe(.childAdded) { (snapshot: FIRDataSnapshot!) in

            let id = (snapshot.value as! NSDictionary)["name"] as! String
            let text = (snapshot.value as! NSDictionary)["text"] as! String
            //let photoUrl = (snapshot.value as! NSDictionary)["photoUrl"] as! String

            self.addMessage(id: id, text: text)
            self.finishReceivingMessage()
        }
    }