ios - 使用谓词过滤字典数组

标签 ios objective-c data-structures nsarray nspredicate

我有以下字典数组:

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

数组中的每个字典都有键pricesize,都是数字。

我想返回一个过滤后的数组,它只包含 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/

相关文章:

ios - CocoaLumberjack 调试和发布的不同日志级别不起作用

ios - 将 NSArray 的内容加载到 UITableView 中

javascript - 如何实现具有一次读取 4 位节点的二进制 trie?

java - Rational 类中相互调用方法

iphone - 如何从 UIView 中删除识别器

ios - 在 IOS 中使用 QuickBlox 视频聊天,无需登录或密码

ios - 在 iOS/Swift 中获取系统正常运行时间

ios - 在 iOS 静态库中公开方法

objective-c - 未调用 UIWebViewDelegate 函数

java - 双向链表中给定节点之间的反向链表 - 算法