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

cho*_*ise 3 objective-c nsdictionary nsarray nspredicate ios

我有NSArray一些NSDictionaries自己也包括一个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];
Run Code Online (Sandbox Code Playgroud)

有没有办法过滤myArray所有图像字典,其中类型是使用NSPredicate fe"覆盖"?

尝试谓词喜欢

predicateWithFormat:@"(SELF.image.type == %@)", @"cover"]
Run Code Online (Sandbox Code Playgroud)

要么

predicateWithFormat:@"(image.type == %@)", @"cover"]
Run Code Online (Sandbox Code Playgroud)

但没有成功.

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

//编辑

所以

NSPredicate *p = [NSPredicate predicateWithFormat:@"image.type == %@", @"cover"];
Run Code Online (Sandbox Code Playgroud)

工作中.但在我的情况下,我想整理尺寸==原始.我做的是

NSPredicate *p = [NSPredicate predicateWithFormat:@"image.size == %@", @"original"];
Run Code Online (Sandbox Code Playgroud)

但随后我的应用程序崩溃了

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSSymbolicExpression length]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)

并指向我的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;
        };
    },
Run Code Online (Sandbox Code Playgroud)

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

jwh*_*hat 6

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