循环遍历NSDictionary以创建单独的NSArrays

时间:2011-01-16 20:12:44

标签: iphone objective-c ios4 nsarray nsdictionary

我有一个很大的NSDictionary我需要循环并创建单独的NSArray。以下是内容:

(
        {
        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";
        };
    }
)

每个部分都有一个类型键,我需要检查。当它找到title键时,我需要将它们添加到数组中。然后,使用gallery密钥的下一部分需要位于其自己的数组中,直到找到另一个title密钥。然后将gallery个密钥放入自己的数组中。

我正在使用UITableView部分标题和内容。因此,上面的NSDictionary应该有一个NSArray *titles;数组,另外两个数组包含标题后面的图库。

我尝试使用for循环,但我似乎无法正确使用它。任何想法都将不胜感激。

3 个答案:

答案 0 :(得分:25)

您的日志有点不清楚,但我猜您的NSDictionary的值为NSDictionary?如果是这样的话:

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.
}

你的问题有点不清楚......但这就是你如何正确地循环NSDictionary

编辑:

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

在此循环之后,galleries将包含NSString类型的键(带有标题的值),以及类型为NSArray的相应对象(带有库的值NSDictionarys })。希望这就是你想要的。

答案 1 :(得分:14)

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

答案 2 :(得分:1)

现代Objective-C语法:

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