Ios 算术运算符与键值编码

Ios 算术运算符与键值编码,ios,objective-c,macos,cocoa,foundation,Ios,Objective C,Macos,Cocoa,Foundation,是否可以使用Obj-C键值编码进行算术运算?我在找这样的东西: [obj valueForKeyPath:@"(val1+val2)"] [obj valueForKeyPath:@"(val1+val2)"]; 或者您必须实现手动添加两个值的属性吗?不可能使用KVC进行算术运算。 您可以使用字符串进行算术运算,然后将其传递给[obj valueforKeyPath:…],如下所示: NSString *val1=@"3"; NSString *val2=@"5"; NSString *f

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

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

或者您必须实现手动添加两个值的属性吗?

不可能使用KVC进行算术运算。

您可以使用字符串进行算术运算,然后将其传递给
[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]);

不可能使用KVC进行运算。

您可以使用字符串进行算术运算,然后将其传递给
[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]);

我使用AKV给出的NSExpression示例创建了这个NSObject类别

这扩展了valueForKeyPath:因此它支持如下表达式:

[obj valueForKeyPath:@"(val1+val2)"]
[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
#导入
@实现NSObject(KVCExtension)
#pragma标记-初始化类
+(空)荷载{
SEL valueForKeyPath=@选择器(valueForKeyPath:);
SEL extendedValueForKeyPath=@选择器(extendedValueForKeyPath:);
方法valueForKeyPathMethod=class_getInstanceMethod([self class],valueForKeyPath);
方法extendedValueForKeyPathMethod=class_getInstanceMethod([self class],extendedValueForKeyPath);
方法\交换实现(valueForKeyPathMethod、extendedValueForKeyPathMethod);
}
#pragma标记-键值编码
-(id)extendedValueForKeyPath:(NSString*)键路径{
/*注意:+将此方法加载到valueForKey:!!!
因此,调用extendedValueForKeyPath:now意味着调用旧的valueForKeyPath:method,反之亦然。
*/
如果([keyPath length]>0&&[keyPath characterAtIndex:0]=='('){
if([self iskindof类:[NSArray类]]){
NSMutableArray*结果=[[NSMutableArray alloc]init];
用于(id obj in(NSArray*)self)
[结果添加对象:[obj valueForKeyPath:keyPath]];
返回[结果副本];
}否则{
NSRegularExpression*regExp=[NSRegularExpression regular expression with pattern:@“[A-Za-z0-9\\.]”
选项:0
错误:无];
NSMutableString*mKeyPath=[keyPath mutableCopy];
[regExp enumerateMatchesInstalling:mKeyPath选项:0范围:NSMakeRange(0,[keyPath长度])使用块:^(NSTextCheckInResult*结果,NSMatchingFlags标志,BOOL*停止){
NSRange range=[[result resultbyadjusting range swithoffset:[mKeyPath length]-[keyPath length]]range];
如果(range.length>0){
NSString*key=[mKeyPath substringWithRange:range];
NSString*val=[[self-extendedValueForKeyPath:key]stringValue];
[mKeyPath replaceCharactersRange:range with string:val];
}
}];
NSExpression*expression=[NSExpression expressionWithFormat:mKeyPath];
返回[expression expressionValueWithObject:nil上下文:nil];
}
}否则
返回[自扩展值forkeypath:keyPath];
}
@结束

我使用AKV给出的NSExpression示例创建了这个NSObject类别

这扩展了valueForKeyPath:因此它支持如下表达式:

[obj valueForKeyPath:@"(val1+val2)"]
[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
#导入
@实现NSObject(KVCExtension)
#pragma标记-初始化类
+(空)荷载{
SEL valueForKeyPath=@选择器(valueForKeyPath:);
SEL extendedValueForKeyPath=@选择器(extendedValueForKeyPath:);
方法valueForKeyPathMethod=class_getInstanceMethod([self class],valueForKeyPath);
方法extendedValueForKeyPathMethod=class_getInstanceMethod([self class],extendedValueForKeyPath);
方法\交换实现(valueForKeyPathMethod、extendedValueForKeyPathMethod);
}
#pragma标记-键值编码
-(id)extendedValueForKeyPath:(NSString*)键路径{
/*注意:+将此方法加载到valueForKey:!!!
因此,调用extendedValueForKeyPath:now意味着调用旧的valueForKeyPath:method,反之亦然。
*/
如果([keyPath length]>0&&[keyPath characterAtIndex:0]=='('){
if([self iskindof类:[NSArray类]]){
NSMutableArray*结果=[[NSMutableArray alloc]init];
用于(id obj in(NSArray*)self)
[结果添加对象:[obj valueForKeyPath:keyPath]];
返回[结果副本];
}否则{
NSRegularExpression*regExp=[NSRegularExpression regular expression with pattern:@“[A-Za-z0-9\\.]”
选项:0
错误:无];
NSMutableString*mKeyPath=[keyPath mutableCopy];
[regExp enumerateMatchesInstalling:mKeyPath选项:0范围:NSMakeRange(0,[keyPath长度])使用块:^(NSTextCheckInResult*结果,NSMatchingFlags标志,BOOL*停止){
NSRange range=[[result resultbyadjusting range swithoffset:[mKeyPath length]-[keyPath length]]range];
如果(range.length>0){
NSString*key=[mKeyPath substringWithRange:range];
NSString*val=[[self-extendedValueForKeyPath:key]stringValue];
[mKeyPath replaceCharactersRange:range with string:val];
}
}];
NSExpression*expression=[NSExpression expressionWithFormat:mKeyPath];
返回[expression expressionValueWithObject:nil上下文:nil];
}
}否则
返回[自扩展值forkeypath:keyPath];
}
@结束

你的确切需求是什么?我不这么认为,伙计。我认为这是你正在提供的字符串变量,并且会被保存。我认为现在你可以自己尝试所有这些。你的确切需求是什么?我不这么认为,伙计。我想这是你正在提供并将被保存的字符串变量。我认为