Ambiguous Index Usage (Swift 3)

I am misusing the index in the following code for this Firebase data stream, but I cannot figure out what I'm doing wrong. I get the error message: Ambiguous use of index for string 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") } } }) } 

I tried to apply what I learned from this post, but I could not understand what I was missing, because I thought I was letting the compiler know what type of dictionary it is with "how? [String: AnyObject]"

Any thoughts or ideas would be greatly appreciated!

0
source share
2 answers

My preferred way to work with data is to deploy FIRDataSnapshot as FIRDataSnapshot as possible.

 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"]!)") } }) 
+3
source

Given Frank's feedback, here is the actual working code I used that follows this approach if useful.

 // 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 { for child in snapshot.children { let data = child as! FIRDataSnapshot let value = data.value! as! [String:Any] self.arrayOfUserSearchHistoryIDs.append(value["Unique ID Event Number"] as! Int) } } }) } 
0
source

All Articles