Reputation: 255
I have created a NSMutableDictonary
.
The problem is that I am getting all the keys sorted into an alphabetical order. Why is it like this?
I have just noticed this today (never checked this before). Is this the actual implementation of NSMutableDictonary
? Is there any way I can get the key/values in the order I have declared?
Below is the code in the screenshot:
- (IBAction)OnBtnSubmit:(id)sender {
NSString *strUid=[[NSUserDefaults standardUserDefaults] objectForKey:@"UID"];
NSString *picData=[self imageToNSString:ProfileImage.image];
NSString *strComment=_commentTxt.text;
NSString *strUnit=_unitTxt.text;
NSMutableDictionary *parameters =[NSMutableDictionary dictionaryWithObjectsAndKeys:strUid,@"uid",strComment,@"comment",strUnit,@"unit_donate",@"hi",@"image",nil];
NSLog(@"parameters : %@",parameters);
My concern is, can I get the key/values in the order I have declared?
Like below:
NSMutableDictionary *parameters =[NSMutableDictionary dictionaryWithObjectsAndKeys:strUid,@"uid",strComment,@"comment",strUnit,@"unit_donate",@"hi",@"image",nil];
{
uid = 85;
comment= test;
unit_donate = 1;
image=hi;
}
I Need this exact order as on the web end there is an array that stores the value for all these key. I need to make sure image
will always be in the 4th position.
But I'm curious to know, I have logged the dictionary that I have created; why it is showing in alphabetical order? Is this the actual order that my dictionary holds or is there any other reason?
Upvotes: 0
Views: 104
Reputation: 2020
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
For more details do check how dictionary works
Upvotes: 0
Reputation: 1535
Yes, It will be like that. But whatever the order is it should not concern with your logic as you are always going to fetch value baesd on key not the based on order.
Upvotes: 2