I have this code
NSDictionary *tempDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:@"authorId","authorName","authorDescription",@"image",nil] forKeys:
[NSArray arrayWithObjects:@"id",@"name",@"desc",@"image",nil]];
NSLog(@"%@",[tempDict description]);
and the output is
desc = authorDescription;
id = authorId;
image = image;
name = authorName;
You see that the dictionary is sorted by key, alphabetically, for some reason. This is not good for me, because I need to add this dictionary to a plist, and this plist already has some dictionaries with unsorted keys. So how can I avoid this sorting?
The sorting is due to
[NSDictionary description], which is used byNSLog. It’s not a fundamental feature ofNSDictionary. If you access the keys through fast enumeration or[dictionary allKeys], you won’t find it sorted.But to your underlying question, if what you want is “unsorted” (random), then “sorted” is just one of the possible random sequences. If you really want unsorted, then sorted shouldn’t matter because its a subset.
If sorting matters, then you don’t mean “unsorted,” you mean “some other sorted order, such as insertion order.” If you need
NSDictionaryto be sorted in some way, you need to impose that by converting it into anNSArray.