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

时间:2011-09-06 13:41:33

标签: objective-c ios nsarray nsdictionary nspredicate

我有一个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];

有没有办法为类型为f.e的所有Image-Dictionaries过滤myArray。使用NSPredicate“封面”?

试过像

这样的谓词
predicateWithFormat:@"(SELF.image.type == %@)", @"cover"]

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

但没有成功。

提前谢谢!如果不清楚则发表评论

//编辑

所以

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

正在运作。但在我的情况下,我想整理尺寸==原始。我做的是

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;
        };
    },

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

以下代码对我有用:

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];

NSPredicate *p = [NSPredicate predicateWithFormat:@"image.type == %@", @"cover"];
NSLog(@"%@", [myArray filteredArrayUsingPredicate:p]);