Loop through NSDictionary to create separate NSArrays

人盡茶涼 提交于 2019-12-03 12:06:02

问题


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.


回答1:


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.




回答2:


NSString *key;
for(key in someDictionary){
     NSLog(@"Key: %@, Value %@", key, [someDictionary objectForKey: key]);
}



回答3:


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"]];
}];


来源:https://stackoverflow.com/questions/4707759/loop-through-nsdictionary-to-create-separate-nsarrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!