我正在使用以下代码中的下标错误地获取此Firebase数据,但我无法弄清楚我做错了什么.我得到一个错误,即对于let uniqueID = each.value [“唯一ID事件编号”]的下标不明确使用!直线.
// 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") } } }) }
我尝试应用我从这个post中学到的东西,但我无法弄清楚我错过了什么,因为我以为我让编译器知道它是什么类型的字典“as?[String:AnyObject]”
任何想法或想法将不胜感激!
我处理数据的首选方法是尽可能晚地打开FIRDataSnapshot.
原文链接:https://www.f2er.com/swift/320088.htmlref!.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"]!)") } })