Arithmetic operators and key coding

Is it possible to do arithmetic using the "Obj-C" value encoding? I'm looking for something like this:

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


or do you need to implement a property that adds two values ​​manually?


source to share

2 answers

Arithmetic using KVC is not possible.

You can do string arithmetic and then pass it [obj valueforKeyPath:...]


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]);




I used the NSExpression example AKV gave to create this NSObject category.

This extends the value of ForKeyPath: so it supports expressions like this:

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


or for example:

[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_\\.]*"

            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];






All Articles