I need to merge two NSDictionary
s into one provided that if there are dictionaries within the dictionaries, they are also merged.
More or less like jQuery's extend
function.
I need to merge two NSDictionary
s into one provided that if there are dictionaries within the dictionaries, they are also merged.
More or less like jQuery's extend
function.
I know this is an old question, but I need to do the same thing: recursively merge two dictionary objects. I need to go a step further and merge any objects that can be merged recursively (the end goal is merging two dictionaries created from plists). I am hosting my solution at https://github.com/bumboarder6/NSDictionary-merge
I am still working on the project, but as of this writing it already works (in limited testing) for recursive dictionary merging. Arrays and Sets are coming soon.
I noticed a few logic errors in some other solutions I have seen for this problem and I hopefully avoided those pitfalls, but critiques are welcome.
Usage is simple:
I came here looking for a copy of jQuery's
extend
but I ended up writing my own implementation. It's a super simple implementation, I did it so I'd understand a way to do it.Hopefully someone will find this helpful, it worked in my tests with deep-recursion. I'm using it to extend deep JSON files full of text.
I added this to the code mentioned above. It may not be fully correct, but it handles the case where 2 dict has an element that 1 dict does not.
}
NSDictionary+Merge.h
NSDictionary+Merge.m
Alexsander Akers works for me except the case where dict2 contains a dictionary that's missing from dict1 - it crashes. I changed the logic to this: