我正在將數據從1格式遷移到另一個格式,以便對我的iOS應用程序之一進行大型更新。這意味着當用戶更新他們的應用程序時,它將他們的數據遷移到新的位置。這個新的地點有更嚴格的規則,並使用適當的做法(基本上,當我第一次創建應用程序,我是一個noob,目前在數據庫中有不好的做法,這個新的位置僅用於良好的數據)。從NSDictionary/Dictionary中刪除所有項目,其中值== -1.0
我有一切都很好地遷移,一切正常,除了我需要做最後一件事。我保存到數據庫的大型字典(它使用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)
}
該計劃然後遍歷該找到的項目數組,並從包含它的數據字典中刪除任何鍵。
這是數據字典的樣子:
"-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";
};
};
你能顯示數據字典它包含什麼? – 3stud1ant3
我用示例更新了我的問題。 – Eli
非常類似於https://stackoverflow.com/questions/31613194/remove-values-from-dictionary-in-ios-swift,但使用Swift的結構 – nathan