Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through NSDictionary to create separate NSArrays

I have a large NSDictionary that I need to loop through and create separate NSArrays. Here are the contents:

(
        {
        id =         {
            text = "";
        };
        sub =         {
            text = " , ";
        };
        text = "";
        "thumb_url" =         {
            text = "";
        };
        title =         {
            text = "2010-2011";
        };
        type =         {
            text = "title";
        };
    },
        {
        id =         {
            text = "76773";
        };
        sub =         {
            text = "December 13, 2010";
        };
        text = "";
        "thumb_url" =         {
            text = "http://www.puc.edu/__data/assets/image/0004/76774/varieties/thumb.jpg";
        };
        title =         {
            text = "College Days - Fall 2010";
        };
        type =         {
            text = "gallery";
        };
    },
        {
        id =         {
            text = "";
        };
        sub =         {
            text = "";
        };
        text = "";
        "thumb_url" =         {
            text = "";
        };
        title =         {
            text = "2009-2010";
        };
        type =         {
            text = "title";
        };
    },
        {
        id =         {
            text = "76302";
        };
        sub =         {
            text = "December 3, 2010";
        };
        text = "";
        "thumb_url" =         {
            text = "http://www.puc.edu/__data/assets/image/0019/76303/varieties/thumb.jpg";
        };
        title =         {
            text = "Christmas Colloquy";
        };
        type =         {
            text = "gallery";
        };
    }
)

Each section has a type key, which I need to check. When it finds the title key, I need to add those to an array. Then the next sections that would use the gallery key needs to be in its own array until it finds another title key. Then the gallery keys after that into their own array.

I am using a UITableView section titles and content. So, the NSDictionary above should have one NSArray *titles; array, and two other arrays each containing the galleries that came after the title.

I have tried using a for loop but I just can't seem to get it right. Any ideas would be appreciated.

like image 335
Nic Hubbard Avatar asked Jan 16 '11 20:01

Nic Hubbard


3 Answers

It's somewhat unclear by your log, but I'm guessing your NSDictionary has values of NSDictionary? If so:

NSMutableArray *titles = [NSMutableArray array];
// etc.

for (id key in sourceDictionary) {
  NSDictionary *subDictionary = [sourceDictionary objectForKey:key];
  if ([subDictionary objectForKey:@"type"] == @"title")
    [titles addObject:[subDictionary objectForKey:@"title"]];
  // etc.
}

Your question is a bit unclear... but this is how you would properly loop through an NSDictionary.

EDIT:

NSMutableDictionary *galleries = [NSMutableDictionary dictionary];
NSString *currentTitle;

for (id key in sourceDictionary) {
  NSDictionary *subDictionary = [sourceDictionary objectForKey:key];
  NSString *type = [subDictionary objectForKey:@"type"];
  if (type == @"title") {
    currentTitle = [subDictionary objectForKey:@"title"];
    if ([galleries objectForKey:currentTitle] == nil)
      [galleries setObject:[NSMutableArray array] forKey:currentTitle];
  } else if (type == @"gallery" && currentTitle != nil)
    [[galleries objectForKey:currentTitle] addObject:subDictionary];
}

After this loop, galleries will contain keys of type NSString (with values of the titles), and corresponding objects of type NSArray (with values of the gallery NSDictionarys). Hopefully this is what you were going for.

like image 154
Skyler Avatar answered Oct 31 '22 12:10

Skyler


NSString *key;
for(key in someDictionary){
     NSLog(@"Key: %@, Value %@", key, [someDictionary objectForKey: key]);
}
like image 14
mafonya Avatar answered Oct 31 '22 13:10

mafonya


Modern Objective-C syntax:

NSMutableArray *things = [NSMutableArray array];
NSMutableArray *stuff = [NSMutableArray array];
NSMutableArray *bits = [NSMutableArray array];

[dictionaries enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [things addObject:[obj valueForKeyPath:@"thing"]];
    [stuff addObject:[obj valueForKeyPath:@"enclosing_object.stuff"]];
    [bits addObject:[obj valueForKeyPath:@"bits"]];
}];
like image 1
Adam Waite Avatar answered Oct 31 '22 14:10

Adam Waite