2013-10-05 87 views
0

我正在掃描字典的NSMutableArray,並向WebService請求每個數據的信息。在NSMutableArray中添加NSMutableDictionary

問題是獲取數據後,我需要更新相同數組字典的2個鍵,當我添加一個對象時,數組繼續爲空。

有什麼問題?

PS:使用ARC

NSMutableArray* arrayOfDicts = [NSMutableArray arrayWithObjects: 
       [NSMutableDictionary dictionaryWithObjectsAndKeys: 
        @"2013",@"year", 
        @"a1beb511-7fe1-434b-ab87-a0d02fb47713",@"yearTB", 
        @"",@"receita_arrecadada", 
        @"",@"receita_prevista_atualizada", 
       nil], 
       [NSMutableDictionary dictionaryWithObjectsAndKeys: 
        @"2012",@"year", 
        @"5d20f841-d790-4671-ae26-505c9a8c7f76",@"yearTB", 
        @"",@"receita_arrecadada", 
        @"",@"receita_prevista_atualizada", 
       nil], 
       nil]; 

__block NSMutableArray* arrayTemp = [[NSMutableArray alloc] init];; 
__block NSMutableDictionary* dicTemp; 

for(dicTemp in arrayOfDicts){ 

    NSString* year = [dicTemp objectForKey:@"year"]; 
    NSString* yearTB = [dicTemp objectForKey:@"yearTB"]; 
    NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; 

    [RequestFacade getReceiptsTotalInYear:[NSString stringWithFormat:@"%@", yearTB] andCompletion:^(Receipt *receipt) { 

     //NSLog(@"%@",receipt.receita_arrecadada); // Print the correct actual value 

     [newDict addEntriesFromDictionary:dicTemp]; 

     [newDict setObject:year forKey:@"year"]; 
     [newDict setObject:yearTB forKey:@"yearTB"]; 
     [newDict setObject:receipt.receita_arrecadada forKey:@"receita_arrecadada"]; 
     [newDict setObject:receipt.receita_atualizada forKey:@"receita_atualizada"]; 

     [arrayTemp addObject:newDict]; 
     NSLog(@"%@", arrayTemp); // shows me the array of dicts 

     // until here, its ok! 
    }]; 
} 
NSLog(@"%@", arrayTemp); // shows me an empty array...F*CK! 
+0

首先初始化arrayOfDict。 – iProgrammer

回答

0

初始化你arrayOfDict

NSMutableArray * arrayOfDict = [[NSMutableArray alloc] init]; 

該數組是空的,因爲您沒有初始化它。寫在上面的這條線上,然後添加

[arrayOfDict arrayWithObjects: <UR ITEMS>]; 

希望它的工作..!快樂編碼

+0

這個var是真實代碼的一個例子。該數組已經初始化。問題是當我添加到新的數組。 –

+0

我認爲你的數組在執行for循環後被釋放,但不知道原因,請嘗試調試它。並嘗試將newArray作爲類屬性。 @屬性(非原子,保留)NSMutableArray * newArray;並使用self.newArray初始化該數組; – iProgrammer

相關問題