NSExpression

The first thing you should know about NSExpression is that it lives to reduce terms. If you think about the process of evaluating an NSPredicate, there are two terms and a comparator, so those two terms need to simplify into something that the operator can handle—very much like the process of compiling a line of code.

nshipster.com/nsexpression/

NSExpression is a class in Cocoa that simplifies complex data tasks. It evaluates mathematical expressions, such as 4 + 5 - 2**3, which gives 1.

It also handles functions. For example, it calculates the standard deviation of numbers like [1, 2, 3, 4, 4, 5, 9, 11], resulting in about 3.22.

Built-in functions include statistics (average, sum, min, max, median, mode, stddev), arithmetic (add, subtract, multiply, divide, modulus, sqrt, log, ln, exp), bounding (ceiling, floor, trunc), binary operations (bitwiseAnd, bitwiseOr, bitwiseXor, leftshift, rightshift, onesComplement), dates (now), strings (lowercase, uppercase), random values, and no-op (noindex).

You can add custom functions, like factorial, by defining them in a category and calling with FUNCTION in the expression format.

NSExpression works in NSPredicate for queries, allowing custom methods like checking locations within a radius for managed objects.


Category:

Year: