只需使用NSPredicate
这样的:
NSString *strFormula1 = @"\"man\" == \"man\" && 1<5 || 12<9";
NSString *strFormula2 = @"\"pac@pac.com\" == \"pac1@pac1.com\" || 9==9 && 8>2 || 2==2";
NSString *strFormula3 = @"\"hello\" == \"world\"";
NSPredicate *predicate = [NSPredicate predicateWithFormat:strFormula1];
BOOL result = [predicate evaluateWithObject:nil]; //True
predicate = [NSPredicate predicateWithFormat:strFormula2];
result = [predicate evaluateWithObject:nil]; //True
predicate = [NSPredicate predicateWithFormat:strFormula3];
result = [predicate evaluateWithObject:nil]; //False
如果您想知道谓词如何解析您的表达式,只需使用:
NSLog(@"%@", [predicate predicateFormat]);
在我们的例子中返回:
("man" == "man" AND 1 < 5) OR 12 < 9 //1st Expression
"pac@pac.com" == "pac1@pac1.com" OR (9 == 9 AND 8 > 2) OR 2 == 2 //2nd Expression
"hello" == "world" //3rd Expression