With fast enumeration and an NSDictionary, iterating in the order of the keys is not guaranteed – how can I make it so it IS in order?

I’m communicating with an API that sends back an NSDictionary as a response with data my app needs (the data is basically a feed). This data is sorted by newest to oldest, with the newest items at the front of the NSDictionary.

When I fast enumerate through them with for (NSString *key in articles) { ... } the order is seemingly random, and thus the order I operate on them isn’t in order from newest to oldest, like I want it to be, but completely random instead.

  • Fast Enumeration on NSArray of Different Types
  • When to use forEach(_:) instead of for in?
  • For…in statement Objective-C
  • In Swift, can I use a for-in enumeration to initialize or reset an array?
  • Type casting in for-in loop
  • Why does fast enumeration not skip the NSNumbers when I specify NSStrings?
  • I’ve read up, and when using fast enumeration with NSDictionary it is not guaranteed to iterate in order through the array.

    However, I need it to. How do I make it iterate through the NSDictionary in the order that NSDictionary is in?

    4 Solutions Collect From Internet About “With fast enumeration and an NSDictionary, iterating in the order of the keys is not guaranteed – how can I make it so it IS in order?”

    One way could be to get all keys in a mutable array:

    NSMutableArray *allKeys = [[dictionary allKeys] mutableCopy];
    

    And then sort the array to your needs:

    [allKeys sortUsingComparator: ....,]; //or another sorting method
    

    You can then iterate over the array (using fast enumeration here keeps the order, I think), and get the dictionary values for the current key:

    for (NSString *key in allKeys) {
       id object = [dictionary objectForKey: key];
       //do your thing with the object 
     }
    

    Dictionaries are, by definition, unordered. If you want to apply an order to the keys, you need to sort the keys.

    NSArray *keys = [articles allKeys];
    NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *key in sortedKeys) {
        // process key
    }
    

    Update the way the keys are sorted to suit your needs.

    You can also directly enumerate like this:

    NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init];
    
    for (NSString* key in xyz) {
        id value = [xyz objectForKey:key];
        // do stuff
    } 
    

    This is taken from the answer provided by zneak in the question: for each loop in objective c for accessing NSMutable dictionary

    As other people said, you cannot garantee order in NSDictionary. And sometimes ordering the allKeys property it’s not what you really want. If what you really want is enumerate your dict by the order your keys were inserted in your dict, you can create a new NSMutableArray property/variable to store your keys, so they will preserve its order.

    Everytime you will insert a new key in the dict, insert it to in your array:

    [articles addObject:someArticle forKey:@"article1"];
    [self.keys addObject:@"article1"];
    

    To enumerate them in order, just do:

    for (NSString *key in self.keys) {
       id object = articles[key];
    }