1

是否可以使用 Obj-C 键值编码进行算术运算?我正在寻找这样的东西:

[obj valueForKeyPath:@"(val1+val2)"]

还是您必须实现一个手动添加这两个值的属性?

4

2 回答 2

3

无法使用 KVC 进行算术运算。

您可以对字符串进行算术运算,然后将其传递给[obj valueforKeyPath:...]as :

NSString *val1=@"3";
NSString *val2=@"5";

NSString *formula = [NSString stringWithFormat:@"%@+%@",val1,val2];

NSExpression *exp = [NSExpression expressionWithFormat:formula];
NSNumber *resultForCustomFormula = [exp expressionValueWithObject:nil context:nil];

NSLog(@"%f", [resultForCustomFormula floatValue]);
于 2013-01-21T10:12:57.447 回答
3

我使用 AKV 提供的 NSExpression 示例来创建这个 NSObject 类别。

这扩展了 valueForKeyPath: 所以它支持这样的表达式:

[obj valueForKeyPath:@"(val1+val2)"];

或例如:

[obj valueForKeyPath:@"@min.(val1+val2)"];

类别

#import <objc/runtime.h>

@implementation NSObject (KVCExtension)

#pragma mark - Initializing a Class

+ (void)load {

    SEL valueForKeyPath = @selector(valueForKeyPath:);
    SEL extendedValueForKeyPath = @selector(extendedValueForKeyPath:);
    Method valueForKeyPathMethod = class_getInstanceMethod([self class], valueForKeyPath);
    Method extendedValueForKeyPathMethod = class_getInstanceMethod([self class], extendedValueForKeyPath);
    method_exchangeImplementations(valueForKeyPathMethod, extendedValueForKeyPathMethod);

}

#pragma mark - Key-Value Coding

- (id)extendedValueForKeyPath:(NSString *)keyPath {

    /* NOTICE: +load exchanged this method with valueForKey: !!!
     Thus calling extendedValueForKeyPath: now means we're calling the old valueForKeyPath: method and vice versa.
    */

    if ([keyPath length] > 0 && [keyPath characterAtIndex:0] == '(') {

        if ([self isKindOfClass:[NSArray class]]) {

            NSMutableArray *results = [[NSMutableArray alloc] init];
            for (id obj in (NSArray *)self)
                [results addObject:[obj valueForKeyPath:keyPath]];
            return [results copy];

        } else {

            NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z0-9_\\.]*"
                                                                    options:0
                                                                                  error:nil];

            NSMutableString *mKeyPath = [keyPath mutableCopy];

            [regExp enumerateMatchesInString:mKeyPath options:0 range:NSMakeRange(0, [keyPath length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

                NSRange range = [[result resultByAdjustingRangesWithOffset:[mKeyPath length] - [keyPath length]] range];
                if (range.length > 0) {

                    NSString *key = [mKeyPath substringWithRange:range];
                    NSString *val = [[self extendedValueForKeyPath:key] stringValue];

                    [mKeyPath replaceCharactersInRange:range withString:val];

                }

            }];

            NSExpression *expression = [NSExpression expressionWithFormat:mKeyPath];
            return [expression expressionValueWithObject:nil context:nil];

        }

    } else
        return [self extendedValueForKeyPath:keyPath];

}

@end
于 2013-01-21T11:29:22.347 回答