nsexpression

Check expression is valid or not

安稳与你 提交于 2019-12-02 11:33:20
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "12+6+ == 1"' I want to validate expression is valid or not. And I am trying this using following code : let equationString = "12+6+" do { let expr = try NSExpression(format: equationString) if let result = expr.expressionValue(with: nil, context: nil) as? NSNumber { let x = result.doubleValue print(x) } else { print("failed") } } catch { print("failed") } I have used try-catch statement, but still I am getting crash here. Is there any solution for this? Any help would be

Catching NSInvalidArgumentException from NSExpression

扶醉桌前 提交于 2019-12-02 02:46:18
问题 In my code I am evaluating strings as mathematical expressions for example: NSString *formula=@"9*7"; NSExpression *expr =[NSExpression expressionWithFormat:formula]; NSLog(@"%@", [[expr expressionValueWithObject:nil context:nil]intValue]); The above works fine but I will be handling dynamic input from users so I need to be able to catch the exception when the user enters faulty data, thus I need to be able to catch the exception in situations like the following: NSString *formula=@"9*"; /

Catching NSInvalidArgumentException from NSExpression

随声附和 提交于 2019-12-01 23:43:21
In my code I am evaluating strings as mathematical expressions for example: NSString *formula=@"9*7"; NSExpression *expr =[NSExpression expressionWithFormat:formula]; NSLog(@"%@", [[expr expressionValueWithObject:nil context:nil]intValue]); The above works fine but I will be handling dynamic input from users so I need to be able to catch the exception when the user enters faulty data, thus I need to be able to catch the exception in situations like the following: NSString *formula=@"9*"; //note the deliberately invalid expression NSExpression *expr =[NSExpression expressionWithFormat:formula];

NSPredicateEditor & NSExpression - Can the display be different than the value for the predicate?

懵懂的女人 提交于 2019-12-01 04:58:26
I have a predicate editor, which the template was generate via the following: NSArray * test = [NSArray arrayWithObjects: [NSExpression expressionForKeyPath: @"Abc"], [NSExpression expressionForKeyPath: @"Def"], nil]; NSPredicateEditorRowTemplate * template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions: test rightExpressionAttributeType: NSStringAttributeType modifier: NSDirectPredicateModifier operators: [NSArray arrayWithObject: [NSNumber numberWithUnsignedInteger:NSContainsPredicateOperatorType]] options:(NSCaseInsensitivePredicateOption

NSPredicateEditor & NSExpression - Can the display be different than the value for the predicate?

懵懂的女人 提交于 2019-12-01 02:20:49
问题 I have a predicate editor, which the template was generate via the following: NSArray * test = [NSArray arrayWithObjects: [NSExpression expressionForKeyPath: @"Abc"], [NSExpression expressionForKeyPath: @"Def"], nil]; NSPredicateEditorRowTemplate * template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions: test rightExpressionAttributeType: NSStringAttributeType modifier: NSDirectPredicateModifier operators: [NSArray arrayWithObject: [NSNumber numberWithUnsignedInteger

Calculate lessthan value using NSExpression?

北战南征 提交于 2019-11-29 17:42:05
For the following block of code: NSString *formul=@"5 < 9"; NSExpression *e = [NSExpression expressionWithFormat:formul]; int result = [[e expressionValueWithObject:nil context:nil] intValue]; NSLog(@"formule:%d", result); I got error: due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "5 < 9 == 1"' You can use NSPredicate instead: NSString *formul = @"15 < 9"; NSPredicate *predicate = [NSPredicate predicateWithFormat:formul]; BOOL b = [predicate evaluateWithObject:nil]; In Swift: let formula = "15 < 9" let predicate = NSPredicate(format: formula

NSExpression 1/2

天涯浪子 提交于 2019-11-28 14:21:35
I want to calculate a string, which I'm doing by this: NSExpression *expression = [NSExpression expressionWithFormat:calculationString]; float result = [[expression expressionValueWithObject:nil context:nil] floatValue]; NSLog(@"%f", result); The problem is, when calculationstring is 1/2, the result is 0. I tried to change float with double and NSNumber and the %f to %f and %@, but I always just get 0. What to I have to change? Also if it matters, I am in Europe, so I have commas instead of points for this value, but it shouldn't matter as I am logging with %f which shows it as points. Just

Chained expressions to perform calculations in Core Data

狂风中的少年 提交于 2019-11-27 21:44:26
First of all: Happy new year :-) What I am trying to do I am trying to divide two attributes in Core Data and then calculate the average of these divisions. The attributes are specified by a key path (e.g. eur , usd , aud ). Example: I have the following data set: date eur usd aud ------------------------------ 2010-01-01 0.5 1.0 1.5 2010-01-02 0.6 1.1 1.6 2010-01-03 0.4 1.0 1.3 Divide two attributes, e.g. eur / usd with the follwowing results... divide eur / usd: ------------------ 2010-01-01 0.5 2010-01-02 0.54 2010-01-03 0.4 ... then calculate the average of these numbers (0.5 + 0.54 + 0.4)

NSExpression 1/2

依然范特西╮ 提交于 2019-11-27 08:29:30
问题 I want to calculate a string, which I'm doing by this: NSExpression *expression = [NSExpression expressionWithFormat:calculationString]; float result = [[expression expressionValueWithObject:nil context:nil] floatValue]; NSLog(@"%f", result); The problem is, when calculationstring is 1/2, the result is 0. I tried to change float with double and NSNumber and the %f to %f and %@, but I always just get 0. What to I have to change? Also if it matters, I am in Europe, so I have commas instead of

Chained expressions to perform calculations in Core Data

♀尐吖头ヾ 提交于 2019-11-27 04:32:38
问题 First of all: Happy new year :-) What I am trying to do I am trying to divide two attributes in Core Data and then calculate the average of these divisions. The attributes are specified by a key path (e.g. eur , usd , aud ). Example: I have the following data set: date eur usd aud ------------------------------ 2010-01-01 0.5 1.0 1.5 2010-01-02 0.6 1.1 1.6 2010-01-03 0.4 1.0 1.3 Divide two attributes, e.g. eur / usd with the follwowing results... divide eur / usd: ------------------ 2010-01