user1525984
user1525984

Reputation:

Can't add NSDictionary to NSArray

I'm trying to add an dictionary to an array to create an array on dictionaries, but when I test the app, the array is still empty. Take a look on my code:

NSMutableArray *listaEstabelecimentosPorCategoria;


    for (NSDictionary *tempDict in listaEstabelecimentos) {

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

            NSLog(@"TEST");
        }
    }

    NSLog(@"%@", listaEstabelecimentosPorCategoria);

The app prints this:

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)

Upvotes: 1

Views: 3081

Answers (3)

PeterParker
PeterParker

Reputation: 308

NSMutableArray *listaEstabelecimentosPorCategoria this code hasn't been allocated to memory so it will be null value

NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];

then listaEstabelecimentosPorCategoria add the object.

Upvotes: -1

Kepler
Kepler

Reputation: 705

NSMutableArray *listaEstabelecimentosPorCategoria=[[NSMutableArray alloc] init]; //initialize


for (NSDictionary *tempDict in listaEstabelecimentos) {

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

        NSLog(@"TEST");
    }
}

NSLog(@"%@", listaEstabelecimentosPorCategoria);

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

when I test the app, the array is still empty.

This is because you did not initialize listaEstabelecimentosPorCategoria:

NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];

Upvotes: 5

Related Questions