综上所述阵列 - Objective-C的 [英] Sum an Array - Objective-C
问题描述
我刚刚起步,我正在寻找一种简单的方法来总结一个简单的数组。我读过插入键值编码苹果开发者网站,我不知道如何应用到我的数组或如果是那样的合适的方式来总结这一点。
I'm just starting out and I'm looking for an easy way to sum a simple array. I've read into apple developer site on key value coding and I don't understand how to apply that to my array or if that's the appropriate way to sum this.
我与键值编码的绊脚石是.keypathToProperty - 那种我可以理解,你需要在一个二维数组进一步的参考,但他们不显示数组code,只有到的keyPath标题行,所以我琢磨不出来呢。
My stumbling block with the key value coding is the .keypathToProperty - I can sort of understand that you need a further reference in a 2D array but they don't show the array code, only the keypath to the title of the row so I can't figure it out yet.
NSMutableArray *numArray = [NSMutableArray arrayWithCapacity:4];
[numArray addObject:num1];
[numArray addObject:num2];
[numArray addObject:num3];
[numArray addObject:num4];
我AP preciate的答复!
I appreciate the replies!
感谢
格雷厄姆
推荐答案
自动方式来做到这一点是:
The automatic way to do it is:
NSNumber * sum = [numArray valueForKeyPath:@"@sum.self"];
但如果你刚刚起步的时候,我会建议避免收集关键路径运营商和去为更简单的方式:
But if you're just starting out, I would recommend avoiding the collection key-path operators and go for the more simple way:
double sum = 0;
for (NSNumber * n in numArray) {
sum += [n doubleValue];
}
这篇关于综上所述阵列 - Objective-C的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!