Ben Ben - 1 year ago 195
Swift Question

Ambiguous Use of Subscript (Swift 3)

I am using the subscript in the following code incorrectly for this Firebase data pull, but I can't figure out what I am doing wrong. I get an error of Ambiguous use of subscript for the

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

print("SnapDict is null")

I tried to applying what I learned from this post, but I couldn't figure out what I am missing because I thought I was letting the compiler know what type of dictionary it is with the "as? [String:AnyObject]"

Any thoughts or ideas would be greatly appreciated!

Answer Source

My preferred way of dealing with data is to unwrap the FIRDataSnapshot as late 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"]!)")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download