Nic*_*ard 12 iphone objective-c nsdictionary nsarray ios4
我有一个庞大的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";
};
}
)
Run Code Online (Sandbox Code Playgroud)
每个部分都有一个类型键,我需要检查.当找到title密钥时,我需要将它们添加到数组中.然后,使用gallery密钥的下一部分需要在其自己的数组中,直到找到另一个title密钥.然后把gallery键放到自己的数组中.
我正在使用UITableView章节标题和内容.因此,NSDictionary上面应该有一个NSArray *titles;数组和另外两个数组,每个数组都包含标题后面的图库.
我试过使用for循环,但我似乎无法正确使用它.任何想法,将不胜感激.
Sky*_*ler 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.
}
Run Code Online (Sandbox Code Playgroud)
你的问题有点不清楚......但这就是你如何正确地循环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];
}
Run Code Online (Sandbox Code Playgroud)
在此循环之后,galleries将包含类型的键NSString(具有标题的值)和类型的相应对象NSArray(具有库的值NSDictionarys).希望这是你想要的.
maf*_*nya 14
NSString *key;
for(key in someDictionary){
NSLog(@"Key: %@, Value %@", key, [someDictionary objectForKey: key]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34875 次 |
| 最近记录: |