I'm trying to combine NSExpression + NSPredicate to create a simple feature where the user will type something like size + 1 > 3
and it will evaluate the expression.
First, to make sure I can get NSExpression to work with a variable, I tried the following:
NSExpression(format: "2 + 1")
.expressionValue(with: nil, context: nil)
// == 3
NSExpression(format: "myInt + 1")
.expressionValue(with: ["myInt": 2], context: nil)
// == 3
Next, to make sure I can evaluate a NSPredicate, I tried the following:
NSPredicate(format: "2 + 1 == 3")
.evaluate(with: nil)
// == true
Now when I try to combine the two, I get error: Execution was interrupted, reason: signal SIGABRT.
, no matter which combination I try:
NSPredicate(format: "size + 1 == 3")
.evaluate(with: ["size": 2])
// error: Execution was interrupted, reason: signal SIGABRT.
NSPredicate(format: "$size + 1 == 3")
.evaluate(with: ["size": 2])
// error: Execution was interrupted, reason: signal SIGABRT.
NSPredicate(format: "size + 1 == 3")
.withSubstitutionVariables(["size": 2])
.evaluate(with: nil)
// error: Execution was interrupted, reason: signal SIGABRT.
NSPredicate(format: "$size + 1 == 3")
.withSubstitutionVariables(["size": 2])
.evaluate(with: nil)
// error: Execution was interrupted, reason: signal SIGABRT.
I know that most NSPredicate's are used to filter lists, which make me wonder if a use-case such as the above could even work.
Is it possible to use a variable in an NSPredicate that is evaluated one-time?