我有以下字典数组:
Printing description of newOrderbookBids:
<__NSArrayI 0x10053e7c0>(
{
price = "10.14";
size = 148;
},
{
price = "10.134";
size = 0;
},
{
price = "10.131";
size = 321;
})
数组中的每个字典都有键price
和size
,都是数字。
我想返回一个过滤后的数组,它只包含 size > 0
的字典。在此示例中,这将是包含字典 #1 和 #3 但没有字典 #2 的数组:
({
price = "10.14";
size = 148;
},
{
price = "10.131";
size = 321;
})
我已尝试使用以下代码片段按大小过滤我的 NSArray *newOrderbookBids
:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"size > 0"];
newOrderbookBids = [newOrderbookBids filteredArrayUsingPredicate:predicate];
不幸的是,我的代码因运行时错误而崩溃
[NSSymbolicExpression compare:]: unrecognized selector sent to instance xxxx
我的代码和谓词有什么问题?
如何按 size > 0
进行过滤?
谢谢!
最佳答案
“SIZE”是 "Predicate Format String Syntax" 中的保留关键字 关键字不区分大小写。
要解决该问题,请使用“%K”格式参数作为 var arg 替换 a 关键路径:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K > 0", @"size"];
关于ios - 使用谓词过滤字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23457801/