Reputation: 16813
I am getting the following error. restaurantData.itemArray
contains array of ProductData
objects and I am trying to filter it with id
as follows. I wonder what I am doing wrong in my implementation.
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.'
+ (NSString *)menuItemForItemId:(NSString *)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;
}
Here is my ProductData
class if it is needed.
ProductData.m
#import "ProductData.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)initWithDictionary:(NSDictionary *)aDict{
self = [self init];
if (self){
self.pId = [aDict objectForKey:@"id"];
self.pPrice = [aDict objectForKey:@"price"];
self.pName = [aDict objectForKey:@"name"];
}
return self;
}
Upvotes: 3
Views: 2726
Reputation: 8106
Your ProductData
does not have a property named id
, from your sample code I can see that it has pId
The line below tries to access a property named id
, which does not exist.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K == %@)", kItemId, itemId];
Upvotes: 2