我正试图向数组中添加一个字典,以在字典上创建一个数组,但当我测试应用程序时,该数组仍然为空。看看我的代码:
NSMutableArray *listaEstabelecimentosPorCategoria;
for (NSDictionary *tempDict in listaEstabelecimentos) {
if ([[tempDict objectForKey:@"category"] integerValue] == 1) {
[listaEstabelecimentosPorCategoria addObject:tempDict];
NSLog(@"TEST");
}
}
NSLog(@"%@", listaEstabelecimentosPorCategoria);
应用程序打印此:
2013-08-18 12:16:38.802 Petrópolis[10157:c07] TEST
2013-08-18 12:16:38.802 Petrópolis[10157:c07] TEST
2013-08-18 12:16:38.803 Petrópolis[10157:c07] (null)
当我测试应用程序时,数组仍然是空的。
这是因为您没有初始化listaEstabelecimentosPorCategoria
:
NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];
NSMutableArray *listaEstabelecimentosPorCategoria=[[NSMutableArray alloc] init]; //initialize
for (NSDictionary *tempDict in listaEstabelecimentos) {
if ([[tempDict objectForKey:@"category"] integerValue] == 1) {
[listaEstabelecimentosPorCategoria addObject:tempDict];
NSLog(@"TEST");
}
}
NSLog(@"%@", listaEstabelecimentosPorCategoria);
NSMutableArray *listaEstabelecimentosPorCategoria
此代码尚未分配给内存,因此它将为空值
NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];
则CCD_ 3添加该对象。