【问题标题】:Loop through NSDictionary to create separate NSArrays循环遍历 NSDictionary 以创建单独的 NSArray
【发布时间】:2011-01-16 20:12:43
【问题描述】:

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

(
        {
        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 循环,但我似乎无法做到正确。任何想法将不胜感激。

【问题讨论】:

    标签: iphone objective-c ios4 nsarray nsdictionary


    【解决方案1】:

    你的日志有点不清楚,但我猜你的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 的值)。希望这就是你想要的。

    【讨论】:

    • 是的,我的 NSDictionary 的值为 NSDicitonary。
    • 如您所见,在每个标题键之后都有一些其他没有使用标题键的字典。因此,如果在标题键字典之后还有 5 个字典,我想将这 5 个放入一个新数组中。然后,如果我看到另一个标题键,则将 NSDictionaries 后面的那些放入一个数组中。
    • 我明白了。我能想到的唯一问题是 NSDictionary 是一个键/值集,不能保证按照您可能期望的顺序。不过,我会相应地编辑我的答案。
    • 我正在从 XML 创建这些字典,所以我很确定顺序会保持不变。
    • 如果可以使用,enumerateWithBlock: 枚举字典的效率明显更高。
    【解决方案2】:
    NSString *key;
    for(key in someDictionary){
         NSLog(@"Key: %@, Value %@", key, [someDictionary objectForKey: key]);
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        相关资源
        最近更新 更多