1

I am trying to insert sample data into core data by fetching it from plist. But that data is not getting inserted in Ordered manner.

Below is my plist structure.

Plist structure

Below is the code which I tried.

NSArray *aArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"SampleData" ofType:@"plist"]];

    for (NSDictionary *aDict in aArray ) 
    {

        [aDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
        {
            //--- UPDTATE -->>
            // while debugging I found one issue that -  
            //Here "obj" has three array - Sample List 1C,Sample List 1A,Sample List 1B
            // This should be Sample List 1A,Sample List 1B,Sample List 1C

            NSDictionary *aSubDict = (NSDictionary *)obj;
            NSMutableArray *aMutCheckpoint = [NSMutableArray array];

            //.… below is my logic for core data insertion…

        }]; 
    }

Can any one please suggest me the way to how do I get ordered values here while inserting data.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
iLearner
  • 1,670
  • 2
  • 19
  • 45

1 Answers1

2

If you want to retrieve values from an NSDictionary in a fixed order then you need to order the keys used to retrieve those values. This is because dictionaries are fundamentally unordered data structures.

So here's one way (there are other ways of sorting the keys):

for (NSDictionary *aDict in aArray) 
{
    NSArray *sortedKeys = [[aDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *key in sortedKeys)
    {        
        id obj = aDict[key];
        // etc.
    }
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242