我想从NSDictionary
中选择并检索所有内容。我有这样的结构
- (void)viewDidLoad {
[super viewDidLoad];
listaOggetti = [[NSMutableArray alloc] init];
NSArray *arrayOne = [NSArray arrayWithObjects: @"First",@"Second",@"Third", nil];
NSArray *sortedOne = [arrayOne sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictOne = [NSDictionary dictionaryWithObject:sortedOne forKey:@"Elementi"];
NSArray *arrayTWo = [NSArray arrayWithObjects:@"First1",@"Second1" ..., nil];
NSArray *sortedTwo = [arrayTwo sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictTwo = [NSDictionary dictionaryWithObject:sortedTWo forKey:@"Elementi"];
NSArray *arrayThree = [NSArray arrayWithObjects:@"First2",@"Second2" ... , nil];
NSArray *sortedThree = [arrayThree sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictThree = [NSDictionary dictionaryWithObject:sortedThree forKey:@"Elementi"];
[listaOggetti addObject:dictOne];
[listaOggetti addObject:dictTwo];
[listaOggetti addObject:dictThree];
}
我想要检索密钥@“Elementi”的所有对象(应该是45左右),以便将它们添加到另一个数组中,例如:
NSDictionary *dict = [listaOggetti objectAtIndex:indexPath.section];
NSArray *array = [dict objectForKey:@"Elementi"];
cellValue = [array objectAtIndex:indexPath.row] ;
(有了这个,dict
在我的项目中只填充了9个对象。)
最后,*数组应该是大约45个对象填充。我试过allValues,但没有用。
我该如何解决?
答案 0 :(得分:1)
最简单的方法是在-viewDidLoad
:
NSMutableArray *allObjects = [[NSMutableArray alloc] init];
[allObjects addObjectsFromArray:sortedOne];
[allObjects addObjectsFromArray:sortedTwo];
[allObjects addObjectsFromArray:sortedThree];
或者,你可以用类似的方式从字典中获取它们:
NSMutableArray *allObjects = [[NSMutableArray alloc] init];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:0] objectForKey@"Elementi"];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:1] objectForKey@"Elementi"];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:2] objectForKey@"Elementi"];
您未能理解的是listaOggetti
是包含三个对象的NSMutableArray
。当你打电话
NSDictionary *dict = [listaOggetti objectAtIndex:indexPath.section];
结果是dict
是一个字典,是listaOggetti
中三个对象之一。因此,当你打电话
NSArray *array = [dict objectForKey:@"Elementi"];
结果是array
是该单个字典@"Elementi"
的密钥dict
的对象。您的代码不会尝试组合三个不同的字典或组合三个数组,每个数组设置为objectForKey:@"Elementi"
三个不同的字典。
如果您想要一个阵列是所有三个不同阵列的串联,那么请使用上面提供的其中一个片段。在这两个片段中,结果是allObjects
是一个NSMutableArray
,按顺序包含所有三个数组。