Dear list,

I need to create a grouped array from a simple array, it then populates a 
grouped UITableView. Below is my code, which works fine (the project uses ARC).

But I'd like to know if there is a quicker or more efficient way to do this, 
with less code. If so, please let me know.

  NSArray *resultArray = [[result objectForKey:@"hits"] objectForKey:@"hits"]; 
//1
  NSMutableSet *dates = [[NSMutableSet alloc] init]; //2
  for (NSDictionary *dict in resultArray) {
        [dates addObject:[[dict objectForKey:@"_source"] 
objectForKey:@"datum_gepubliceerd_ymd"]]; //3
  }
  self.datesArray = [NSArray arrayWithArray:[[dates allObjects] 
sortedArrayUsingComparator:^(NSString* a, NSString* b) {
                    return [b compare:a options:NSNumericSearch]; //4
  }]];
  self.sortedJSONContent = [NSMutableArray array]; //5
  for (NSString *date in self.datesArray) {
        NSMutableArray *sortedSection = [NSMutableArray array]; //6
        for (NSDictionary *dict in resultArray) {
                if ([[[dict objectForKey:@"_source"] 
objectForKey:@"datum_gepubliceerd_ymd"] isEqual:date])
                [sortedSection addObject:dict]; //6
        }
        [self.sortedJSONContent addObject:sortedSection]; //7
        }


These are the steps the above code takes:

1. Create the resultArray from a JSON web service, this is an array of 
dictionaries (result is returned from a NSJSONSerialization action);
2. Create a mutableSet to hold the group titles (these will go in the 
tableView's sectionHeader views);
3. Iterate through the resultArray and add the relevant key's value to the 
mutableSet;
4. Create a datesArray from the set, ordered descending (creating an array of 
unique values);
5. Create a new sortedJSONContent array to hold the grouped array when done;
6. Loop through the dates array (main loop), and with each iteration create a 
sortedSection array loop through the resultArray ('nested loop') and add those 
items for which the date is equal to the main loop's date (I don't need to 
worry about empty arrays here, because they only get created when a date 
exists, which means a record exists and, therefore, that the array will have at 
least one item);
7. Add the sortedSection array to the new sortedJSONContent array.

The output is an array of arrays that fits nicely into the grouped UITableView.

PS I know this is done much more easily using CoreData, I actually have another 
app that does just that. But in this case, all data is pulled in from the 
network at all times and there is no persistent storage on the device. For this 
reason, implementing CoreData feels like overkill to me.

Thanks!

Diederik
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to