使用 NSPredicate 使用 2D-NSDictionaries 过滤 NSArray
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
以下代码对我有用:
The following code worked for me: