Ambiguous Use of Subscript (Swift 3)

寵の児 提交于 2019-12-17 21:23:05

问题


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 line.

// 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 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!


回答1:


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



回答2:


Taking Frank's feedback into account, here is the actual working code I used that follows that approach in case it's helpful.

// 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)
                 }
            }
   })
}


来源:https://stackoverflow.com/questions/40090406/ambiguous-use-of-subscript-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!