1

对于以下代码块:

NSString *formul=@"5 < 9";

NSExpression *e = [NSExpression expressionWithFormat:formul];

int result = [[e expressionValueWithObject:nil context:nil] intValue];

NSLog(@"formule:%d", result);

我收到错误:

由于未捕获的异常'NSInvalidArgumentException',原因:'无法解析格式字符串“5 < 9 == 1”'

4

1 回答 1

3

您可以NSPredicate改用:

NSString *formul = @"15 < 9";
NSPredicate *predicate = [NSPredicate predicateWithFormat:formul];
BOOL b = [predicate evaluateWithObject:nil];

斯威夫特:

let formula = "15 < 9"
let predicate = NSPredicate(format: formula)
let b = predicate.evaluate(with: nil)
print(b) // false
于 2013-09-18T11:41:19.787 回答