我有以下Objective-C class :

@interface GraphDataPoint : NSObject
@property NSDate *date;
@property NSNumber *value;
@end

给定NSArrayGraphDataPoint,我知道如何使用来计算最大value
[data valueForKeyPath:@"@max.value"]

现在我想将value更改为values数组:
@property NSArray *values;

有没有类似的方法可以使用value计算GraphDataPoint中每个NSArrayvalueForKeyPath属性的总和的最大值? (我知道我可以编写一个嵌套循环来手动进行计算。)

最佳答案

您应该可以这样做:

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/

10-09 10:21