0

I have an atomfeed with which is succesfully getting parsed. All the articles are in the root array. But now I also have categories as a tag in the feed.

How can I parse the atom xml so that the root array contains the categories and each category filters to an array with the corresponding articles.

Thanks, Bing

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
bing
  • 443
  • 6
  • 11

1 Answers1

1

If I understand you correctly, you have an NSArray (you didn't say of what, so I'll assume NSDictionary) of Atom feed items, each of which has a category. What you want is a structure that groups the items by category into NSArrays.

The appropriate data structure, in my opinion, is an NSDictionary that maps categories onto arrays of items. Here's how I would do it:

NSMutableDictionary *itemsByCategory = [NSMutableDictionary dictionary];
for (NSDictionary *item in allItems) {
    NSString *category = [item objectForKey:@"category"];
    NSMutableArray *categoryItems = [itemsByCategory objectForKey:category]; // You'll probably want to do some checking in case there is no category.

    if (!categoryItems) {
        categoryItems = [NSMutableArray array];
        [itemsByCategory setObject:categoryItems forKey:category];
    }

    [categoryItems addObject:item];
}
Alex
  • 26,829
  • 3
  • 55
  • 74
  • One more question, I now have a table view with the categories, is there a way to reuse this tableview with the category result items. I have been trying in the didSelectRowAtIndexPath to create a new table view with the results. Thank you Bing – bing Oct 21 '09 at 12:44