objective-c - 使用 NSPredicate 使用 2D-NSDictionaries 过滤 NSArray

标签 objective-c ios nsarray nsdictionary nspredicate

我有一个包含一些 NSDictionariesNSArray,它们本身也包含一个 NSDictionary

NSDictionary *dict1 = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:@"cover" forKey:@"type"] forKey:@"image"];
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:@"cover" forKey:@"type"] forKey:@"image"];
NSDictionary *dict3 = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:@"back" forKey:@"type"] forKey:@"image"];
NSDictionary *dict4 = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:@"cover" forKey:@"type"] forKey:@"image"];

NSArray *myArray = [NSArray arrayWithObjects:dict1, dict2, dict3, dict4, nil];

有没有办法为所有类型为 f.e. 的图像字典过滤 myArray使用 NSPredicate“覆盖”?

试过像这样的谓词

predicateWithFormat:@"(SELF.image.type == %@)", @"cover"]

predicateWithFormat:@"(image.type == %@)", @"cover"]

但没有成功。

提前致谢!如果不清楚,请发表评论

//编辑

所以

NSPredicate *p = [NSPredicate predicateWithFormat:@"image.type == %@", @"cover"];

正在工作。但就我而言,我想整理出 size == original。我做的是

NSPredicate *p = [NSPredicate predicateWithFormat:@"image.size == %@", @"original"];

但随后我的应用崩溃了

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSSymbolicExpression length]: unrecognized selector sent to instance

并指向我的 filteredArrayUsingPredicate 方法。我看不出类型和大小有什么区别。在这里查看我的阵列的 NSLog

 (
        {
        image =         {
            height = 1500;
            id = 4e5808765e73d607350059b4;
            size = original;
            type = poster;
            url = "someurl";
            width = 1000;
        };
    },
        {
        image =         {
            height = 750;
            id = 4e5808765e73d607350059b4;
            size = mid;
            type = poster;
            url = "someurl";
            width = 500;
        };
    },

有人知道为什么当我尝试使用大小而不是类型时它会崩溃吗?

最佳答案

SIZE 是保留关键字。

Predicate Programming Guide 的底部找到:

保留以下字词:

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

关于objective-c - 使用 NSPredicate 使用 2D-NSDictionaries 过滤 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7320887/

相关文章:

ios - 具有 selectedSegmentIndex 的 cellForRowAtIndexPath 中的 NSRangeException

ios - Xcode 停止在设备上工作

ios - 如何访问数组内部类的方法

IOS/objective-C : Save Custom Array as NSUserDefault

objective-c - 您如何确定自定义 UINavigationItem.titleView 的大小/框架?

iphone - 强制文本垂直放置在 UILabel 中?

ios - tableView 不刷新

iphone - gzipInflate/gzipDeflate 错误

iphone - 阵列清理方法错误

ios - 打开应用程序 3 次后显示 uialertview?