2011-01-16 8 views
12

ループを作成して別の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は1つのNSArray *titles;配列と、タイトルの後に来るギャラリーをそれぞれ含む2つの他の配列を持つ必要があります。

私はforループを使用しようとしましたが、正しくできないようです。任意のアイデアをいただければ幸いです。

答えて

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を通してどのように考え、適切にループです。

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

galleriesは(タイトルの値を有する)タイプNSStringのキーが含まれ、タイプNSArrayのオブジェクトに対応する(ギャラリーNSDictionarysの値を有します)。うまくいけば、これはあなたが行っていたものです。

+0

はい、私のNSDictionaryのはNSDicitonaryの値を持っています。 –

+0

ご覧のとおり、各タイトルキーの後には、タイトルキーを使用しない他のいくつかの辞書があります。だから、さらに5つの辞書がタイトルキー辞書の後に来たら、私はそれらを5つの新しい配列に入れたいと思う。次に、別のタイトルキーが表示されたら、それに続くNSDictionariesを配列に入れます。 –

+0

私は参照してください。私が考えることができる唯一の問題は、NSDictionaryがあなたが期待している順序であることが保証されていないキー/値のセットであることです。それでも、私はそれに応じて私の答えを編集します。 – Skyler

14
NSString *key; 
for(key in someDictionary){ 
    NSLog(@"Key: %@, Value %@", key, [someDictionary objectForKey: key]); 
} 
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"]]; 
}]; 
関連する問題