Question

I have the following array of dictionaries:

Printing description of newOrderbookBids:
<__NSArrayI 0x10053e7c0>(
{
    price = "10.14";
    size = 148;
},
{
    price = "10.134";
    size = 0;
},
{
    price = "10.131";
    size = 321;
})

Each dictionary in the array has the keys price and size, both are numbers.

I would like to return a filtered array which contains only the dictionaries for which size > 0. In this example, this would be the array with dictionary #1 and #3, but without dictionary #2:

   ({
        price = "10.14";
        size = 148;
    },
    {
        price = "10.131";
        size = 321;
    })

I have tried the following code snippet to filter my NSArray *newOrderbookBids by size:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"size > 0"];
newOrderbookBids = [newOrderbookBids filteredArrayUsingPredicate:predicate];

Unfortunately my code crashes with the runtime error

[NSSymbolicExpression compare:]: unrecognized selector sent to instance xxxx

What is wrong with my code and predicate? How can I filter by size > 0?

Thank you!

Was it helpful?

Solution

"SIZE" is a reserved keyword in the "Predicate Format String Syntax" and the keywords are case-insensitive.

To solve that problem, use the "%K" format argument as a var arg substitution for a key path:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K > 0", @"size"];

OTHER TIPS

Reserved Words

The following words are reserved:

AND, OR, IN, NOT, ALL, ANY, SOME, NONE, LIKE, CASEINSENSITIVE, CI, MATCHES, CONTAINS, BEGINSWITH, ENDSWITH, BETWEEN, NULL, NIL, SELF, TRUE, YES, FALSE, NO, FIRST, LAST, SIZE, ANYKEY, SUBQUERY, CAST, TRUEPREDICATE, FALSEPREDICATE

A clearer error message when this happens would be nice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top