0

NSMutableArray から NSDictionary を抽出し、その辞書からオブジェクトを抽出する必要があります。コードは非常に簡単なはずですが、NSDictionary 宣言で SIGABRT エラーが発生し続けます。

-(void)calcolaConto {
        conto = [[NSNumber alloc] initWithDouble:0];
    for (int i=0; [shoppingListItems count]; ++i) {
        NSDictionary *dictVar = (NSDictionary *) [shoppingListItems objectAtIndex:i]; //<-- SIGABRT
        NSNumber *IO = (NSNumber *) [dictVar objectForKey:@"incout"];
        NSNumber *priceValue = (NSNumber *) [dictVar objectForKey:@"price"];
        if ([IO isEqualToNumber:[NSNumber numberWithInt:0]]) {
            conto = [NSNumber numberWithDouble:([conto doubleValue] + [priceValue doubleValue])];
        } else if ([IO isEqualToNumber:[NSNumber numberWithInt:1]]) {
            conto = [NSNumber numberWithDouble:([conto doubleValue] - [priceValue doubleValue])];
        }
        NSLog(@"Valore %@", conto);
    }
}

「shoppingListItems」は次のように作成されます。

    NSMutableDictionary *rowDict = [[NSMutableDictionary alloc] initWithCapacity:6];
    [rowDict setObject:primaryKeyValue forKey: ID];
    [rowDict setObject:itemValue forKey: ITEM];
    [rowDict setObject:priceValue forKey: PRICE];
    [rowDict setObject:groupValue forKey: GROUP_ID];
    [rowDict setObject:incOut forKey:INC_OUT];
    [rowDict setObject:dateValue forKey: DATE_ADDED];
    [shoppingListItems addObject: rowDict];
4

1 に答える 1

2

問題は、ループが停止しないことです。次を使用する必要があります。

for (NSUInteger i = 0; i < [shoppingListItems count]; i++) {

また:

for (NSDictionary* dictVar in shoppingListItems) {

範囲外の要素にアクセスしようとしないようにします。現在のループでは、配列の終わりを超える[shoppingListItems count]に達するまでiがインクリメントされるため、objectAtIndexは例外をスローします。

于 2011-10-16T15:34:33.223 に答える