0

I am migrating my data from 1 format to another for a large update to one of my iOS apps. This means that when a user updates their application it migrates their data to a new location. This new location has much stricter rules and uses proper practices (basically when I first created the app I was a noob and there are bad practices currently in the database and this new location is for good data only).

I have everything migrating nicely and everything works except I need to do one last thing. The large dictionary that I save to the database (it uses firebase fyi) has -1.0 stored everywhere there should be a nil value (bad, I know). What I need to do is loop through the entire dictionary and remove any key where the value of that key is -1.0.

The Dictionary is of type [AnyHashable : Any] which is what firebase uses.

I have tried this so far.

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

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

   print(foundItems)
}

the plan was to then loop through that found items array and remove any key from the data dictionary that contained it.

This is what the data dictionary looks like:

"-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";
    };
};

and this is what it should look like

"-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";
    };
};
Eli
  • 668
  • 2
  • 13
  • 37

1 Answers1

0

The solution I have is to map (iterate) over each parent key and assign it's child nodes to a variable child. Then test each child property (assignmentGoal, assignmentName etc) for either "-1" for the string and -1 for the ints. If it's a match, remove that key: value pair from child.

Then assign child back to the parent node.

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

and the result output

(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])
Jay
  • 34,438
  • 18
  • 52
  • 81