对于以下代码块:
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”“
发布于 2013-09-18 11:41:19
您可以使用NSPredicate代替:
NSString *formul = @"15 < 9";
NSPredicate *predicate = [NSPredicate predicateWithFormat:formul];
BOOL b = [predicate evaluateWithObject:nil];在Swift中:
let formula = "15 < 9"
let predicate = NSPredicate(format: formula)
let b = predicate.evaluate(with: nil)
print(b) // falsehttps://stackoverflow.com/questions/18871071
复制相似问题