2013-08-18 49 views
1

我想添加一個字典到數組中以在字典上創建數組,但是當我測試應用程序時,數組仍然是空的。就以我的代碼來看看:無法將NSDictionary添加到NSArray

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) 
+2

你忘了初始化你的數組。 –

回答

6

當我測試的應用程序,該陣列仍然是空的。

這是因爲你沒有初始化listaEstabelecimentosPorCategoria

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


for (NSDictionary *tempDict in listaEstabelecimentos) { 

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

     NSLog(@"TEST"); 
    } 
} 

NSLog(@"%@", listaEstabelecimentosPorCategoria); 
+0

你在NSMutableArray之前忘了額外的括號 –

+0

對不起,我改正了它。 – Kepler

0

NSMutableArray *listaEstabelecimentosPorCategoria這段代碼還沒有被分配到內存中,因此這將是空值

NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array]; 

然後listaEstabelecimentosPorCategoria添加對象。