我有一个包含一些 NSDictionaries
的 NSArray
,它们本身也包含一个 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/