【发布时间】:2011-11-11 08:42:12
【问题描述】:
我有一个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. 的所有图像词典过滤 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;
};
},
有人知道为什么当我尝试使用大小而不是类型时它会崩溃吗?
【问题讨论】:
标签: objective-c ios nsarray nsdictionary nspredicate