Ios Can';t使用Firebase数据库中的数据填充数组

Ios Can';t使用Firebase数据库中的数据填充数组,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,下面我尝试创建一个数组,但它不起作用 let chatRef = FIRDatabase.database().reference().child("chatListings") override func viewDidLoad() { super.viewDidLoad() let firstQuery = chatRef.queryOrdered(byChild: "userID").queryEqual(toValue: userID) firstQuery.

下面我尝试创建一个数组,但它不起作用

let chatRef = FIRDatabase.database().reference().child("chatListings")


override func viewDidLoad() {
    super.viewDidLoad()
    let firstQuery = chatRef.queryOrdered(byChild: "userID").queryEqual(toValue: userID)
    firstQuery.observe(FIRDataEventType.value, with: { snapshot in
        for child in snapshot.children {
            print("child is \(child)")
            if let dict = snapshot.value as? Dictionary<String, AnyObject>{
                print("dict is \(dict)")
                let roomKey = dict["chatRoomKey"] as! String
                let oUID = dict["otherUserID"] as! String
                let oUserName = dict["otherUserName"] as! String
                let oProfilePic = dict["otherUserProfilePic"] as! String
                let userIDTemp = dict["userID"] as! String
                chatListing = ChatListing(chatRoomKey: roomKey, UID: userIDTemp, name: oUserName, otherUserID: oUID, otherUserProfilePicURL: oProfilePic)
                chatListings.append(chatListing)
            }
        }
        print("chatListings = \(chatListings)")
    })
}
我正在尝试提取除消息部分以外的所有数据,我计划稍后在应用程序中进行提取

此数据(不包括“消息”部分)写入chatViewController的viewDidLoad中,如下所示:

    let preMessageRef = chatRef.childByAutoId()
    chatListingID = preMessageRef.key
    let initialChatRoomData = ["chatRoomKey": chatListingID, "otherUserID": otherUID, "otherUserName": otherUserName, "otherUserProfilePic": otherUserProfilePicURLString, "userID": userID]
    preMessageRef.setValue(initialChatRoomData)

从Firebase数据库检索数据对我来说完全是一帆风顺,复制成功的数据提取尝试很少能成功两次。他们的文档很少,以至于遗漏了太多内容,因为它对如何在真实环境中提取数据几乎没有帮助。为什么人们喜欢Firebase?与Firebase合作是一次非常令人沮丧的经历,我对此感到非常遗憾。但是现在回头去做更好的事情可能已经太晚了,例如,一个为如何让它工作提供清晰指导的平台。

我认为你只是有一个愚蠢的打字错误。试试这个:

let childData = child as! FIRDataSnapshot
print("child key: \(childData.key)")
if let dict = childData.value as? Dictionary<String, AnyObject> {
    ...
}
让childData=childas!FIRDataSnapshot
打印(“子项:\(childData.key)”)
如果让dict=childData.value为?字典{
...
}
也就是说,使用
child
而不是
snapshot



更新。使用
NSDictionary
,而不是
Dictionary
,修复了
dict
常量崩溃。但是,除了编译器错误,仍然不清楚为什么…

init?(快照:FIRDataSnapshot)
中,到底发生了什么?因为有很多返回值为零,其中一个可能会导致问题。什么有效,什么无效?崩溃?怎么,那完全是另一回事!显示错误消息和值。您可以发布崩溃消息吗?您正在将所有值强制展开为
String
s,您确定它们实际上都是
String
对象吗?在您发布的数据中,
otherUserID=aRandomUser3611
otherUserName=Michael
看起来不像
String
对象。如果它们不是String对象,那么它们还能是什么呢?那些显示它正在写入Firebase数据库的值都是String。但我看到有些是引用的,而另一些不是。我不知道为什么会发生这种情况,也不知道这意味着什么。编译器首先响应一个fix-it错误,使其在被接受后的读取方式如下:if let dict=(child as AnyObject)。value as?字典然后说这是对.value的模糊使用。所以我希望这个改变能解决它,但它似乎没有。我试过了,但它在它下面的一行没有解释就崩溃了。当我注释掉那一行并再试一次时,它在下面的那一行崩溃了。无法告诉您原因。打印输出正确的childByAutoID值,这是我要提取的值之一。随着您的更改,它仍然会在“dict”试图提取值的任何地方崩溃,甚至在它刚要打印时也会崩溃。同样,编译器没有解释。
let childData = child as! FIRDataSnapshot
print("child key: \(childData.key)")
if let dict = childData.value as? Dictionary<String, AnyObject> {
    ...
}