我有以下设置:
@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
和
#import <Foundation/Foundation.h>
#import "Item.h"
@interface Category : Item {
}
- (NSString *)path;
@end
我有一个包含类别实例(称为“类别”)的数组,我想根据它的 item_id 取出一个项目。这是我为此使用的代码:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"item_id == %d", 1];
NSArray *filteredArray = [categories filteredArrayUsingPredicate:predicate];
这会导致以下错误:
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key item_id.'
我该如何解决这个问题,我做错了什么?属性已合成,我可以在 Category 实例上成功访问和设置 item_id 属性。
最佳答案
您已将 item_id
属性声明为指针。但是 NSInteger
是标量类型(32 位或 64 位整数),因此您应该将其声明为
@property (nonatomic, assign) NSInteger item_id;
备注:从 LLVM 4.0 编译器(Xcode 4.4)开始,@synthesize
和实例变量都是自动生成的。
关于objective-c - 基于父类(super class)中的 NSInteger 属性使用 NSPredicate 过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16035775/