本文介绍了下标的歧义使用 (Swift 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此 Firebase 数据拉取,我错误地使用了以下代码中的下标,但我无法弄清楚我做错了什么.我收到一个错误:let uniqueID = each.value["Unique ID Event Number"] as!Int 行.

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

我尝试应用我从这篇帖子中学到的知识,但我无法弄清楚我错过了什么,因为我以为我让编译器知道它是什么类型的字典,带有as?[String:AnyObject]"

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!

推荐答案

我处理数据的首选方式是尽可能晚地打开 FIRDataSnapshot.

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

这篇关于下标的歧义使用 (Swift 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:13