Question

Is it possible to do arithmetics using Obj-C Key-Value Coding? I am looking for something like this:

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

or do you have to implement a property that adds the two values manually?

Was it helpful?

Solution

Arithmetic using KVC is not possible.

You can do arithmetic with strings and then pass it to [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]);

OTHER TIPS

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

This extends valueForKeyPath: so it supports expressions like this:

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

or per example:

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

Category

#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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top