对于此Firebase数据提取,我在以下代码中错误地使用了下标,但是我无法弄清楚自己在做什么错。我在let uniqueID = each.value["Unique ID Event Number"] as! Int一行中使用下标模棱两可的错误。
let uniqueID = each.value["Unique ID Event Number"] as! Int
// Log user in if let user = FIRAuth.auth()?.currentUser { let uid = user.uid // values for vars sevenDaysAgo and oneDayAgo set here ... let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)") historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in if (snapshot.value is NSNull) { print("user data not found") } else { if let snapDict = snapshot.value as? [String:AnyObject] { for each in snapDict { // Save the IDs to array. let uniqueID = each.value["Unique ID Event Number"] as! Int self.arrayOfUserSearchHistoryIDs.append(uniqueID) } } else{ print("SnapDict is null") } } }) }
我尝试应用从这篇文章中学到的知识,但是我无法弄清楚我所缺少的内容,因为我想让编译器知道“ as?[String:AnyObject]”是什么类型的字典。
任何想法或想法将不胜感激!
我处理数据的首选方式是FIRDataSnapshot尽可能早地拆开数据。
FIRDataSnapshot
ref!.observe(.value, with: { (snapshot) in for child in snapshot.children { let msg = child as! FIRDataSnapshot print("\(msg.key): \(msg.value!)") let val = msg.value! as! [String:Any] print("\(val["name"]!): \(val["message"]!)") } })