下标的模糊使用(Swift 3)

时间:2016-10-17 15:34:45

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

我正在使用以下代码中的下标错误地获取此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?[字符串:AnyObject]“

非常感谢任何想法或想法!

2 个答案:

答案 0 :(得分:3)

我处理数据的首选方法是尽可能晚地展开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"]!)") } })

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateMilestone([Bind(Include = "Name,Description,FromDate,DueDate,Finished,ProjectID")] Milestone milestone, int? id)
    {
        if (ModelState.IsValid)
        {
            var forProject = db.Projects.Where(x => x.ID == id).FirstOrDefault();
            if (forProject != null)
            {
                milestone.Project = forProject;
                db.Milestones.Add(milestone);
                db.SaveChanges();
                return RedirectToAction("Details", forProject);
            }
            else
            {
                return HttpNotFound();
            }
        }
        else
        {
            return View(milestone);
        }
    }

答案 1 :(得分:0)

考虑到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)
                 }
            }
   })
}