【发布时间】:2017-04-10 04:00:34
【问题描述】:
我有一个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;
}
【问题讨论】:
-
你可以试试这个吗? pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pCategory == %@", @"4"]]];
-
是的,效果很好。
标签: ios objective-c nspredicate