Reputation: 2473
how is it possbile to output the values of a dictionary in the order of input.
Example:
My Input:
[dicValue0 setObject:@"Start Date & Time" forKey:@"START_DATETIME"];
[dicValue0 setObject:@"Specify End" forKey:@"SPECIFY_END"];
[dicValue0 setObject:@"End Date & Time" forKey:@"END_DATETIME"];
[dicValue0 setObject:@"Open End" forKey:@"END_OPEN"];
Outputs:
I know how a dictionary works, but I want the output in the same order as the input!
I can write a loop which sorts me the output in the order of the input. But if i had 10000+ values that's not the best and performant way. Is there anything from apple, that helps me with this problem?
Upvotes: 3
Views: 3804
Reputation: 7976
I am pretty sure that dictionaries are not keeping track of the input order. How are you outputting the dictionary, looping through keys or just printing the dictionary?
If you know the order you want to retrieve the objects, you can create your own version of the keys array and loop through that to pull out the objects from the dictionary in your desired order
Upvotes: 3
Reputation: 21882
You should create an array that keeps track of the order of the inserted keys, then iterate through that to pull values out of the dictionary. There's no built-in way to have an ordered dictionary in Foundation.
Upvotes: 5