NSDictionary's allKeys array messed up - not in the order they are in the dictionary?
Asked Answered
H

5

5

in my project I'm pulling data from a .plist, more concretely from an NSDictionary. I init a dictionary with the contents of the .plist, which works well. When I then do

NSArray *array = [dict allKeys];

it fills the array with all the keys, but in a totally random order, different to the order they are in the .plist file. I would really need to preserve the order. The keys in the dictionary are arrays, if that could cause a problem. What am I not getting?

Thanks a lot in advance!

Hume answered 1/5, 2012 at 16:32 Comment(0)
P
7

Much like there is no order between items in an NSSet, there's is no inherent order to the key-value pairs within an NSDictionary.

If you want keys under a specific order, you'd need to use something like - (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr but that won't give the order in which the NSDictionary was serialized in the plist file.

Pascual answered 1/5, 2012 at 16:36 Comment(4)
so the answer is, it is not possible to do what I want?Hume
Of course it's possible, it's just not as easy as you're hoping. See my answer for one way to accomplish what you seem to be trying to accomplish.Costumier
If you want "What is the order in which key-values are stored in the plist file?" then the short answer is "no". Long answer: Not unless you want the burden of manually parsing the plist file. NSDictionaries hold unordered key-values pairs. NSArrays hold ordered items. If you're the one storing and reading back the data, perhaps you need to use NSArray to store your data.Pascual
Little unclear . Nice answer but still needs more explanationAtonement
L
6

There is no such inbuilt method from which you can acquire this. But a simple logic work for you. You can simply add few numeric text in front of each key while you prepare the dictionary. Like

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
                       @"01.Created",@"cre",
                       @"02.Being Assigned",@"bea",
                       @"03.Rejected",@"rej",
                       @"04.Assigned",@"ass",
                       @"05.Scheduled",@"sch",
                       @"06.En Route",@"inr",
                       @"07.On Job Site",@"ojs",
                       @"08.In Progress",@"inp",
                       @"09.On Hold",@"onh",
                       @"10.Completed",@"com",
                       @"11.Closed",@"clo",
                       @"12.Cancelled", @"can",
                       nil]; 

Now if you can use sortingArrayUsingSelector while getting all keys in the same order as you place.

 NSArray *arr =  [[dict allKeys] sortedArrayUsingSelector:@selector(localizedStandardCompare:)];

At the place where you want to display keys in UIView, just chop off the front 3 character.

Ladonnalady answered 22/2, 2013 at 9:10 Comment(0)
C
3

As Qwerty Bob said, NSDictionary doesn't order its contents. If you need to persist an order for the keys, one way to do that would be to separately store an array of the keys in your .plist file. Then, enumerate the array and use that to access the dictionary values in order.

Costumier answered 1/5, 2012 at 16:41 Comment(0)
S
2

You can create an NSArray with the Dictionary keys in the order you want.

Scutt answered 19/6, 2012 at 15:38 Comment(1)
Can you please give an example?Lighthearted
P
-1

Try this:

NSArray *array = [dict allKeys];

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];

NSArray *orderedKeys = [array sortedArrayUsingDescriptors:descriptors];
Postorbital answered 19/12, 2014 at 1:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.