objective-c - 基于父类(super class)中的 NSInteger 属性使用 NSPredicate 过滤对象数组

标签 objective-c nsarray nspredicate kvc

我有以下设置:

@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/

相关文章:

ios - 我不明白为什么 Objective-C 委托(delegate)函数可以工作而 Swift 委托(delegate)函数会崩溃。你能给我解释一下吗?

java - 为不同的设备生成服务器端图像?

objective-c - 初始化对象/ARC

ios - 使用 NSPredicate 过滤 NSArray

ios - NSPredicate 在 CoreData 中按字母顺序获取记录

swift - 在 swift 3 中创建复杂的 NSCompoundPredicate

ios - 与 NSDate 比较相关的核心数据谓词

ios - 自定义 UITableViewCell 在编辑时不显示缩进

iOS 10 : Random Crashes in dispatch_async

ios - 更新 NSarray 内特定键的值