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

发布于 2024-12-03 07:54:54 字数 2137 浏览 1 评论 0原文

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

有没有一种方法可以使用 NSPredicate 过滤类型为 fe“cover”的所有图像字典的 myArray

尝试过类似

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

or 的

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

有人知道为什么当我尝试使用 size 而不是 type 时它会崩溃吗?

i have a NSArray containing some NSDictionaries which themselves also include a 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];

is there a way to filter myArray for all Image-Dictionaries where the type is f.e. "cover" using a NSPredicate?

tried predicates like

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

or

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

but without success.

thanks in advance! leave a comment if something is unclear

// edit

so

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

is working. but in my case i want to sort out size == original. what i did is

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

but then my app crashes with

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

and pointing to my filteredArrayUsingPredicate method. i cannot see any difference between type and size. see my NSLog of my Array here

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

anybody knows why it crashes when i try to use size instead of type ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2024-12-10 07:54:54

SIZE 是保留关键字。

位于 谓词编程指南

保留以下单词:

AND、OR、IN、NOT、ALL、ANY、SOME、NONE、 LIKE、不区分大小写、CI、匹配、包含、BEGINSWITH、ENDSWITH、BETWEEN、NULL、NIL、SELF、TRUE、YES、FALSE、NO、FIRST、LAST、SIZE、ANYKEY、SUBQUERY、CAST、TRUEPREDICATE、FALSEPREDICATE

SIZE is a reserved keyword.

Found at the bottom of the Predicate Programming Guide:

The following words are reserved:

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

没有心的人 2024-12-10 07:54:54

以下代码对我有用:

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

The following code worked for me:

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]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文