Objective-c问题通过循环遍历数组

Bod*_*dar 0 int core-data objective-c nsarray ios

我有这个代码,它接受来自Core Data的收入对象数组.

- (void)totalIncome:(NSMutableArray *)IncomesArray {
    int i;
    int total;
    for (i = 0; i < [IncomesArray count]; ++i)
    {
        Income *income = [IncomesArray objectAtIndex:i];

        total += (int)[income value];
        NSLog(@"%@", total);
    }
    self.totalIncomes = [[NSNumber alloc] initWithInt:(int)total];
    NSLog(@"%.2f", self.totalIncomes);
}
Run Code Online (Sandbox Code Playgroud)

但行NSLog(@"%@",总计); 导致EXEC BAD ACCESS错误.有没有明显的事我做错了.此外,如果我删除日志,则没有添加到totalIncomes中,在我的头文件中声明为NSNumber.谢谢.

Dav*_*ong 7

这个怎么样:

- (void)totalIncome:(NSMutableArray *)IncomesArray {
    NSNumber *total = [IncomesArray valueForKeyPath:@"@sum.value"];
    NSLog(@"total: %@", total);
}
Run Code Online (Sandbox Code Playgroud)