2012-09-10 61 views

回答

1

看看this發表於newacct

NSString *equation = @"10 + 20 - 5 * 7 + (20/5)"; 

// dummy predicate that contains our expression 
NSPredicate *pred = [NSPredicate predicateWithFormat: 
         [equation stringByAppendingString:@" == 42"]];//42 is just a rightExpression to make it an equation 
NSExpression *exp = [pred leftExpression]; 
NSNumber *result = [(NSComparisonPredicate *)exp expressionValueWithObject:nil context:nil]; 
NSLog(@"%@", result); // logs "-1" 
+0

謝謝!這是我正在尋找的東西。 –