【问题标题】:NSPRedicate valueForUndefinedKeyNSPRedicate valueForUndefinedKey
【发布时间】: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


【解决方案1】:

您应该给出您计划用来过滤数组的对象的属性名称。这里我猜它是 pCategory。你应该重写你的代码如下

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多