在NSMutableArray中添加NSMutableDictionary

时间:2013-10-05 06:18:04

标签: ios objective-c nsmutablearray nsmutabledictionary

我正在扫描字典的NSMutableArray,并向WebService请求每个数据的信息。

问题是在获取数据之后,我需要更新相同数组字典的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!

1 个答案:

答案 0 :(得分:0)

初始化您的arrayOfDict

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

数组为空,因为您没有初始化它。将此上面的行写在顶部,然后添加

[arrayOfDict arrayWithObjects: <UR ITEMS>];

希望它有效.. !!快乐编码