我有以下Objective-C class :
@interface GraphDataPoint : NSObject
@property NSDate *date;
@property NSNumber *value;
@end
给定
NSArray
的GraphDataPoint
,我知道如何使用来计算最大value
[data valueForKeyPath:@"@max.value"]
现在我想将
value
更改为values
数组:@property NSArray *values;
有没有类似的方法可以使用
value
计算GraphDataPoint
中每个NSArray
的valueForKeyPath
属性的总和的最大值? (我知道我可以编写一个嵌套循环来手动进行计算。) 最佳答案
您应该可以这样做:
NSArray *a = @[
[[GraphDataPoint alloc] initWithVal:@[@1, @2, @3]] // Sum=6
, [[GraphDataPoint alloc] initWithVal:@[@4, @1, @1]] // Sum=6
, [[GraphDataPoint alloc] initWithVal:@[@5, @4, @7]] // Sum=16
, [[GraphDataPoint alloc] initWithVal:@[@6, @8, @9]] // Sum=23 <<== Max
, [[GraphDataPoint alloc] initWithVal:@[@7, @6, @2]] // Sum=15
];
NSNumber *res = [a valueForKeyPath:@"@[email protected]"];
NSLog(@"Res: %@", res); // This prints 23
关于ios - valueForKeyPath可以计算总和的最大值吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23875294/