ios - 从 Parse 数据库循环 JSON 给出错误的结果

标签 ios json swift uitableview dictionary

Swift 2.0, xcode 7.1

我正在尝试从 Parse 数据库中检索一些数据,过滤它以删除重复项并存储在字典中。每行解析都有客户下的订单(如下所示的 JSON),我想在 UITableView 中检索它以显示下的订单。如果客户最近下了多个订单,我想对其进行过滤并在其客户 ID 下的表格 View 的一个部分中显示他的所有订单。

过滤正常,但出于某种原因,我的循环没有给我准确的结果。

解析第 1 行:

[{"Customer":"9sKSDTG7GY","Product":"Burger","Quantity":"2"}]

解析第 2 行:

[{"Customer":"nyRHskbTwG","Product":"Sizzler","Quantity":"2"},{"Customer":"nyRHskbTwG","Product":"Biryani","Quantity":"2"}]

检索此数据并存储在 self.custome、self.fQuantity 和 self.fName 变量中。

我使用的循环如下:

let cD = self.customer
                print("Customer data before filtering Unique value: \(self.customer)")
                self.uniqueValues = self.uniq(cD) //Calling a function to get unique values in customer data

  print("Customer data after filtering Unique value: \(self.uniqueValues)")
            var newArray = [[String]]()

            for var count = 0; count < self.customer.count; count++ {
                for sID in self.uniqueValues {
                    if sID.containsString(self.customer[count]){

                        let dicValue = [String(self.fQuantity[count]), String(self.fName[count])]
                        newArray.append(dicValue)

                        self.dicArray.updateValue(newArray, forKey: sID)
                    } else {
                        // Do nothing...
                    }
                }
            }

            print("Dictionary Values: \(Array(self.dicArray.values))")
            print("Dictionary Keys: \(Array(self.dicArray.keys))")

打印输出如下:

Customer data before filtering Unique value: ["9sKSDTG7GY", "nyRHskbTwG", "nyRHskbTwG"]

Customer data after filtering Unique value: ["9sKSDTG7GY", "nyRHskbTwG"]

Dictionary Values: [[["2", "Burger"], ["2", "Sizzler"], ["2", "Biryani"]], [["2", "Burger"]]]

Dictionary Keys: ["nyRHskbTwG", "9sKSDTG7GY"]

有人能找出我做错了什么吗?

最佳答案

正如@David 建议的那样,您必须交换外循环和内循环。但如果在 if 循环中找不到任何内容,我还必须删除 newArray 中包含的所有值。这就是我让它发挥作用的方式。

let cD = self.customer
                print("Customer data before filtering Unique value: \(self.customer)")
                self.uniqueValues = self.uniq(cD) //Calling a function to get unique values in customer data

  print("Customer data after filtering Unique value: \(self.uniqueValues)")
        var newArray = [[String]]()

        for sID in self.uniqueValues {
 for var count = 0; count < self.customer.count; count++ {
                if sID.containsString(self.customer[count]){

                    let dicValue = [String(self.fQuantity[count]), String(self.fName[count])]
                    newArray.append(dicValue)

                    self.dicArray.updateValue(newArray, forKey: sID)
                } else {
                   newArray.removeAll() // ****** Adding this works for me

                }
            }
        }

        print("Dictionary Values: \(Array(self.dicArray.values))")
        print("Dictionary Keys: \(Array(self.dicArray.keys))")

关于ios - 从 Parse 数据库循环 JSON 给出错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33505777/

相关文章:

android - 如何在 POST 后以正确的编码在 Flask 中返回 json

javascript - 从 JSON 设置父子关系

arrays - 如何使用 JSON 文件中的数据填充数组

ios - tableview 上的持久数据(存储在 coredata 中)问题

swift - UITableViewCell 函数

ios - 调整 UIImage 的大小需要很长时间?

iphone - 获取无效上下文错误

ios - 比较集合中的元素并通过检查属性删除

ios - 更新到 Xcode 6 后 : Undefined symbols for architecture armv7: "___gnu_f2h_ieee"

javascript - 引用错误: email is not defined