1

我正在尝试将字典添加到数组以在字典上创建数组,但是当我测试应用程序时,数组仍然为空。看看我的代码:

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)
4

3 回答 3

5

当我测试应用程序时,数组仍然是空的。

这是因为您没有初始化listaEstabelecimentosPorCategoria

NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];
于 2013-08-18T15:24:18.037 回答
2
NSMutableArray *listaEstabelecimentosPorCategoria=[[NSMutableArray alloc] init]; //initialize


for (NSDictionary *tempDict in listaEstabelecimentos) {

    if ([[tempDict objectForKey:@"category"] integerValue] == 1) {
        [listaEstabelecimentosPorCategoria addObject:tempDict];

        NSLog(@"TEST");
    }
}

NSLog(@"%@", listaEstabelecimentosPorCategoria);
于 2013-08-18T15:29:38.897 回答
-1

NSMutableArray *listaEstabelecimentosPorCategoria此代码尚未分配到内存,因此它将为空值

NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];

然后listaEstabelecimentosPorCategoria添加对象。

于 2013-08-19T07:07:40.217 回答