我有一个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>,
)


产品数据

#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;
}

最佳答案

您应提供计划用来过滤数组的对象的属性名称。我猜这里是pCategory。您应该按照以下方式重写代码

pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pCategory == %@", @"4"]]];

关于ios - NSPRedicatevalueForUndefinedKey,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43314759/

10-09 12:28