【问题标题】:Ambiguous Use of Subscript (Swift 3)下标的歧义使用(Swift 3)
【发布时间】:2017-02-26 16:11:18
【问题描述】:

我在以下代码中错误地使用了此 Firebase 数据提取中的下标,但我不知道我做错了什么。我收到 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")
                    }
                }
       })
}

我尝试应用从 post 中学到的知识,但我无法弄清楚我缺少什么,因为我认为我让编译器知道它是什么类型的字典与“as?[字符串:任何对象]"

任何想法或想法将不胜感激!

【问题讨论】:

    标签: swift dictionary firebase firebase-realtime-database subscript


    【解决方案1】:

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

    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】:

    考虑到 Frank 的反馈,以下是我使用的实际工作代码,它遵循该方法,以防有用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 2017-03-07
      • 1970-01-01
      相关资源
      最近更新 更多