我有一个大NSDictionary
我需要循环并创建单独的NSArray
s。以下是内容:
(
{
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
key 需要位于自己的数组中,直到找到另一个title
钥匙。然后gallery
之后的键放入自己的数组中。
我正在使用一个UITableView
章节标题和内容。所以NSDictionary
上面应该有一个NSArray *titles;
数组,以及另外两个数组,每个数组包含标题后面的画廊。
我尝试过使用for
循环但我似乎无法做到正确。任何想法,将不胜感激。
你的日志有点不清楚,但我猜你的NSDictionary
的值为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.
}
你的问题有点不清楚......但这就是你正确循环的方式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
)。希望这就是您想要的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)