我收到以下错误。 restaurantData.itemArray
包含 ProductData
对象的数组,我正在尝试使用 id
对其进行过滤,如下所示。我想知道我在实现中做错了什么。
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.'
+ (NSString *)menuItemForItemIdNSString *)itemId
{
ProductData *restaurantData = [ProductData restaurantDataInstance];
NSString *item = @"";
NSPredicate *predicate = [NSPredicate predicateWithFormat"(%K == %@)", kItemId, itemId];
// the error is thrown in the following line
NSArray *filteredArray = [restaurantData.itemArray filteredArrayUsingPredicate:predicate];
if ([filteredArray count] > 0)
item = [(NSDictionary *)[filteredArray objectAtIndex:0] objectForKey:kItem];
return item;
}
如果需要,这是我的 ProductData
类。
ProductData.m
#import "roductData.h"
#define kTitleKey @"pName"
#define kPriceKey @"price"
#define kIdKey @"id"
@implementation ProductData
@synthesize pId, pImage, pPrice, pName, itemArray;
+(ProductData*) restaurantDataInstance {
static ProductData *restaurantDataInstance;
@synchronized(self) {
if(!restaurantDataInstance){
restaurantDataInstance = [[ProductData alloc] init];
}
}
return restaurantDataInstance;
}
- (id)init
{
if (self = [super init]) {
if (!itemArray || !itemArray.count){
itemArray = [NSMutableArray arrayWithCapacity:10];
}
}
return self;
}
-(id)initWithDictionaryNSDictionary *)aDict{
self = [self init];
if (self){
self.pId = [aDict objectForKey"id"];
self.pPrice = [aDict objectForKey"price"];
self.pName = [aDict objectForKey"name"];
}
return self;
}
您的 ProductData
没有名为 id
的属性,从您的示例代码中我可以看到它具有 pId
下面的行尝试访问名为 id
的属性,该属性不存在。
NSPredicate *predicate = [NSPredicate predicateWithFormat"(%K == %@)", kItemId, itemId];
关于ios - 此类与键的键值编码不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43688342/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |