gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:08:04 25 4
gpt4 key购买 nike

我有这段代码,它接受来自 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);
}

但是行 NSLog(@"%@", total);导致 EXEC BAD ACCESS 错误。有什么明显的我做错了吗?此外,如果我删除日志,则不会向 totalIncomes 添加任何内容,totalIncomes 在我的头文件中声明为 NSNumber。谢谢。

最佳答案

这个怎么样:

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

关于Objective-c 问题通过循环遍历数组来求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5173287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com