Ios NSPRedicate UNDEFINEDKEY的值
我有一个ProductData数组,如下所示,我想根据它们的类别对它们进行过滤,我应用了以下谓词,但它返回了错误Ios NSPRedicate UNDEFINEDKEY的值,ios,objective-c,nspredicate,Ios,Objective C,Nspredicate,我有一个ProductData数组,如下所示,我想根据它们的类别对它们进行过滤,我应用了以下谓词,但它返回了错误 pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category = %@", @"4"]]]; 由于未捕获异常“NSUnknownKeyException”而终止应
pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category = %@", @"4"]]];
由于未捕获异常“NSUnknownKeyException”而终止应用程序,
原因:'[valueForUndefinedKey:]:此类
不符合密钥类别的密钥值编码。'
这是佩利昂的俯瞰图
您应该提供用于过滤数组的对象的属性名称。在这里,我想这是pCategory。你应该重写你的代码如下
Ptemplements=[[NSMutableArray alloc]initWithArray:[self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@PCCategory==%@,@4]] 你能试试这个吗?Ptemplements=[[NSMutableArray alloc]initWithArray:[self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@PCCategory==%@,@4]];是的,效果很好。
po self.pElements
<__NSArrayM 0x174241e30>(
<ProductData: 0x17427ce00>,
<ProductData: 0x17427cd80>,
<ProductData: 0x17427ce40>,
<ProductData: 0x17427ce80>,
)
#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;
}