I have a NSDictionary, which looks like:
0 = {
content = Test;
date = "2012-02-02 18:36:46 +0000";
};
1 = {
content = "#### da ####";
date = "2012-02-02 18:36:46 +0000";
};
2 = {
content = dfdffdfddfdffddf;
date = "2012-02-03 20:30:31 +0000";
};
But if I delete the second key (number 1 in the example) the dictionary ends up like:
0 = {
content = Test;
date = "2012-02-02 18:36:46 +0000";
};
2 = {
content = dfdffdfddfdffddf;
date = "2012-02-03 20:30:31 +0000";
};
How could I reorganizate them so it maintains the order (like 0, 1, 2, 3) when I need to?
NSDictionaries are unordered, meaning that you cannot control the order of keys and they may change randomly or in undocumented ways between iOS versions.
You have a few options:
Maintain your own, separate NSArray of keys, and use that to index your dictionary.
Use this OrderedDictionary class that I wrote, which basically does that but wraps it all up in an NSDictionary subclass so you don’t have to do any extra work:
http://charcoaldesign.co.uk/source/cocoa#ordered-dictionary
If your dictionary keys are numbers (or if they are alphabetical, or something else that’s easy to sort automatically), you could just sort the keys whenever you need to display them in order using: