循环遍历NSDictionary以创建单独的NSArrays [英] Loop through NSDictionary to create separate NSArrays
问题描述
我有一个大的 NSDictionary
,我需要循环并创建单独的 NSArray
。以下是内容:
I have a large NSDictionary
that I need to loop through and create separate NSArray
s. Here are the contents:
(
{
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
键放入他们自己的数组中。
Each section has a type key, which I need to check. When it finds the title
key, I need to add those to an array. Then the next sections that would use the gallery
key needs to be in its own array until it finds another title
key. Then the gallery
keys after that into their own array.
我使用的是 UITableView
部分标题和内容。因此,上面的 NSDictionary
应该有一个 NSArray * titles;
数组,以及另外两个数组,每个数组都包含来自的库在标题之后。
I am using a UITableView
section titles and content. So, the NSDictionary
above should have one NSArray *titles;
array, and two other arrays each containing the galleries that came after the title.
我尝试使用进行
循环,但我似乎无法正确使用它。任何想法都将不胜感激。
I have tried using a for
loop but I just can't seem to get it right. Any ideas would be appreciated.
推荐答案
您的日志有点不清楚,但我猜你的 NSDictionary
的值为 NSDictionary
?如果是这样的话:
It's somewhat unclear by your log, but I'm guessing your NSDictionary
has values of 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
。
Your question is a bit unclear... but this is how you would properly loop through an 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];
}
此循环后,画廊
将包含类型为 NSString
的键(带有标题的值),以及类型为 NSArray
的相应对象(带有库的值 NSDictionarys
)。希望这就是你想要的。
After this loop, galleries
will contain keys of type NSString
(with values of the titles), and corresponding objects of type NSArray
(with values of the gallery NSDictionarys
). Hopefully this is what you were going for.
这篇关于循环遍历NSDictionary以创建单独的NSArrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!