我有一个ProductData
数组,如下所示,我想根据他们的category
过滤它们,我应用了以下Predicate
,但它返回错误。
pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category = %@", @"4"]]];
由于未捕获的异常'NSUnknownKeyException'而终止应用, 原因:'[valueForUndefinedKey:]:这个类 不是关键类别的关键值编码兼容。'
这里忽略了pElements
po self.pElements
<__NSArrayM 0x174241e30>(
<ProductData: 0x17427ce00>,
<ProductData: 0x17427cd80>,
<ProductData: 0x17427ce40>,
<ProductData: 0x17427ce80>,
)
ProductData.m
#import "ProductData.h"
@implementation ProductData
@synthesize pId, pImage, pPrice, pName, pCategory
-(id)initWithDictionary:(NSDictionary *)aDict{
self = [self init];
if (self){
self.pId = [aDict objectForKey:@"id"];
self.pPrice = [aDict objectForKey:@"price"];
self.pImage = [aDict objectForKey:@"imagePath"];
self.pCategory = [aDict objectForKey:@"category"];
self.pName = [aDict objectForKey:@"name"];
}
return self;
}
答案 0 :(得分:1)
您应该提供计划过滤数组的对象的属性名称。在这里,我想这是pCategory。您应该重写您的代码,如下所示
pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pCategory == %@", @"4"]]];