从NSDictionary / Dictionary中删除所有项目,其中value == -1.0

时间:2017-08-26 03:06:55

标签: swift dictionary firebase nsdictionary

我正在将我的数据从1种格式迁移到另一种格式,以便对我的某个iOS应用进行大量更新。这意味着当用户更新其应用程序时,它会将其数据迁移到新位置。这个新位置有更严格的规则,并使用适当的做法(基本上当我第一次创建应用程序时,我是一个菜鸟,目前数据库中存在不良做法,这个新位置仅用于获得良好数据)。

我有一切都很好地迁移,一切正常,除了我需要做最后一件事。我保存到数据库的大字典(它使用firebase fyi)有-1.0存储在哪里应该有一个零值(坏,我知道)。我需要做的是遍历整个字典并删除该键的值为-1.0的任何键。

字典的类型为[AnyHashable:Any],这是firebase使用的。

到目前为止我已尝试过这个。

if let data = dataDictionary as? [AnyHashable : Any] {

   let foundItems = data.filter { $0.value as? Double == -1.0 }

   print(foundItems)
}

然后计划循环遍历找到的items数组,并从包含它的数据字典中删除任何键。

这就是数据字典的样子:

"-KpIdh_TQMG4fyfFgkdt" =                 {
                assignments =                     {
                    "-KpIgH6uN19OpcuedYe1" =                         {
                        assignmentGoal = "-1";
                        assignmentName = "Information System Proposal";
                        assignmentResult = 100;
                        assignmentWeight = 5;
                    };
                    "-KpIgJnFlC6fhgS0NWxF" =                         {
                        assignmentGoal = "-1";
                        assignmentName = "Information System";
                        assignmentResult = "-1";
                        assignmentWeight = 35;
                    };
                    "-KpIgOGSAwg_VSpDWhWR" =                         {
                        assignmentGoal = "-1";
                        assignmentName = "Process Analysis";
                        assignmentResult = "-1";
                        assignmentWeight = 30;
                    };
                    "-KpIgPhu_3Zxw36xt3O4" =                         {
                        assignmentGoal = "-1";
                        assignmentName = Labs;
                        assignmentResult = "-1";
                        assignmentWeight = 10;
                    };
                    "-KpIgQoFEdRnLlMAq2VN" =                         {
                        assignmentGoal = "-1";
                        assignmentName = Exam;
                        assignmentResult = "-1";
                        assignmentWeight = 20;
                    };
                };
                paperColor = 22;
                paperGoal = 95;
                paperName = "Systems Analysis";
            };
        };
        semesterCode = 17S2;
        semesterGoal = 90;
        semesterName = "2017 Semester Two";
    };
};

这就是它应该是什么样子

"-KpIdh_TQMG4fyfFgkdt" =                 {
                assignments =                     {
                    "-KpIgH6uN19OpcuedYe1" =                         {
                        assignmentName = "Information System Proposal";
                        assignmentResult = 100;
                        assignmentWeight = 5;
                    };
                    "-KpIgJnFlC6fhgS0NWxF" =                         {
                        assignmentName = "Information System";
                        assignmentWeight = 35;
                    };
                    "-KpIgOGSAwg_VSpDWhWR" =                         {
                        assignmentName = "Process Analysis";
                        assignmentWeight = 30;
                    };
                    "-KpIgPhu_3Zxw36xt3O4" =                         {
                        assignmentName = Labs;
                        assignmentWeight = 10;
                    };
                    "-KpIgQoFEdRnLlMAq2VN" =                         {
                        assignmentName = Exam;
                        assignmentWeight = 20;
                    };
                };
                paperColor = 22;
                paperGoal = 95;
                paperName = "Systems Analysis";
            };
        };
        semesterCode = 17S2;
        semesterGoal = 90;
        semesterName = "2017 Semester Two";
    };
};

1 个答案:

答案 0 :(得分:0)

我的解决方案是在每个父键上映射(迭代)并将其子节点分配给变量 child 。然后测试每个子属性(assignmentGoal,assignmentName等)为" -1"对于字符串和-1为整数。如果匹配,请从中删除该键:值对。

然后将子节点分配回父节点。

let result: [Any] = dict.map { dictionary in
    var d = dictionary
    var child = d.value as Dictionary

    if let goal = child["assignmentGoal"] as? String, goal == "-1" {
        child.removeValue(forKey: "assignmentGoal")
    }

    if let name = child["assignmentName"] as? String, name == "-1" {
        child.removeValue(forKey: "assignmentName")
    }

    if let result = child["assignmentResult"] as? Int, result == -1 {
        child.removeValue(forKey: "assignmentResult")
    }

    if let weight = child["assignmentWeight"] as? Int, weight == -1 {
        child.removeValue(forKey: "assignmentWeight")
    }

    d.value = child

    return d
}

//show the output
for item in result {
    print(item)
}

和结果输出

(key: "-KpIgOGSAwg_VSpDWhWR", 
    value: ["assignmentName": "Process Analysis System", "assignmentWeight": 30])
(key: "-KpIgQoFEdRnLlMAq2VN",
   value: ["assignmentName": "Exam", "assignmentWeight": 20])
(key: "-KpIgJnFlC6fhgS0NWxF",
   value: ["assignmentName": "Information System", "assignmentGoal": "3", "assignmentWeight": 35])
(key: "-KpIgPhu_3Zxw36xt3O4",
   value: ["assignmentName": "Labs", "assignmentWeight": 10])
(key: "-KpIgH6uN19OpcuedYe1",
   value: ["assignmentName": "Information System Proposal", "assignmentResult": 100, "assignmentWeight": 5])