4

I've got the following setup:

@interface Item : NSObject {
    NSInteger *item_id;
    NSString *title;
    UIImage *item_icon;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) NSInteger *item_id;
@property (nonatomic, strong) UIImage *item_icon;

- (NSString *)path;

- (id)initWithDictionary:(NSDictionary *)dictionairy;

@end

And

#import <Foundation/Foundation.h>
#import "Item.h"

@interface Category : Item {

}

- (NSString *)path;

@end

I've got an array with Category instances (called 'categories') that I'd like to take a single item out based on it's item_id. Here is the code I use for that:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"item_id == %d", 1]; 
NSArray *filteredArray = [categories filteredArrayUsingPredicate:predicate];

This leads to the following error:

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

How can I fix this and what am I doing wrong? the properties are synthesized and I can acces and set the item_id property successfully on Category instances.

Gidogeek
  • 1,277
  • 2
  • 16
  • 27
  • NSPredicate *predicate = [NSPredicate predicateWithFormat:@"item_id == 1"]; NSArray *filteredArray = [categories filteredArrayUsingPredicate:predicate]; – Manu Apr 16 '13 at 11:26
  • @Manohar the 1, is not static, it's actually a NSInteger which is variable, I left the setting of the variable outside the example since it should not affect the question or the answer. Even though I gave your solution a try, and it gives the same error – Gidogeek Apr 16 '13 at 11:34
  • Have a look at to my answer – Manu Apr 16 '13 at 11:39
  • 1
    Are you sure that you want the `item_id` property as a *pointer*? `NSInteger` is not an object, so perhaps you want just `NSInteger item_id`. – Martin R Apr 16 '13 at 11:40
  • Thank you @MartinR that fixed it. Not sure why I added that as a pointer (3 years ago). Thanks! Make sure you add an answer so I can mark it as resolved. – Gidogeek Apr 16 '13 at 11:45

3 Answers3

4

You have declared the item_id property as a pointer. But NSInteger is a scalar type (32-bit or 64-bit integer), so you should declare it as

@property (nonatomic, assign) NSInteger item_id;

Remark: Starting with the LLVM 4.0 compiler (Xcode 4.4), both @synthesize and the instance variables are generated automatically.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
3

The first thing is that NSArray cannot contain primitive type of objects like 1, 2, 3. But, does contain object. So, when you create a predicate you should create it in the same way as such that it takes object. The above predicate should be reformed to something like this to work;

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"item_id == %@", @(1)];
Sandeep
  • 20,908
  • 7
  • 66
  • 106
3
NSString *str = [NSString stringWithFormat:@"%i",yourIntVariable];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"item_id == %@", str]; 
NSArray *filteredArray = [categories filteredArrayUsingPredicate:predicate];
Manu
  • 4,730
  • 2
  • 20
  • 45
  • This does not work. The issue is elsewhere: "this class is not key value coding-compliant for the key item_id." as mentioned in the question. The predicate looks correct to me as well, hence my confusion. When filtering on another items, which isn't inherited from the base class, it works perfectly. I believe the issue is in the KVC setup, not in the Predicate. But that's more a educated guess. – Gidogeek Apr 16 '13 at 11:41