根据超类中的NSInteger属性使用NSPredicate过滤对象数组 [英] Filter Array of Objects with NSPredicate based on NSInteger property in super class

查看:171
本文介绍了根据超类中的NSInteger属性使用NSPredicate过滤对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

@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

我有一个包含Category实例(称为"categories")的数组,我想根据它的item_id取出一个项目.这是我使用的代码:

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];

这会导致以下错误:

* 由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:此类是 不符合键值编码的键item_id.'

* 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属性.

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.

推荐答案

您已将item_id属性声明为指针.但是NSInteger是标量类型(32位或64位整数),因此应将其声明为

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;

备注:从LLVM 4.0编译器(Xcode 4.4)开始,@synthesize和实例变量都会自动生成.

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

这篇关于根据超类中的NSInteger属性使用NSPredicate过滤对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆