gpt4 book ai didi

ios - NSPRedicate valueForUndefinedKey

转载 作者:行者123 更新时间:2023-11-29 00:24:50 25 4
gpt4 key购买 nike

我有一个 ProductData 数组,如下所示,我想根据它们的 category 过滤它们,我应用了以下 Predicate,但它返回错误。

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

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key category.'

这里忽略了 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;
}

最佳答案

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com