Reputation: 5634
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!
Upvotes: 1
Views: 1122
Reputation: 11546
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.
Upvotes: 1
Reputation: 539685
"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"];
Upvotes: 1