【发布时间】:2013-04-08 18:47:40
【问题描述】:
我有以下设置:
@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];
这会导致以下错误:
* 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:这个类是 键 item_id 的键值编码不兼容。'
我该如何解决这个问题,我做错了什么?属性已合成,我可以在 Category 实例上成功访问和设置 item_id 属性。
【问题讨论】:
-
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"item_id == 1"]; NSArray *filteredArray = [类别filteredArrayUsingPredicate:predicate];
-
@Manohar the 1,不是静态的,它实际上是一个可变的 NSInteger,我将变量的设置留在了示例之外,因为它不应该影响问题或答案。即使我尝试了您的解决方案,但它给出了相同的错误
-
看看我的回答
-
您确定要将
item_id属性用作指针吗?NSInteger不是一个对象,所以也许你只想要NSInteger item_id。 -
感谢@MartinR 修复了它。不知道为什么我将其添加为指针(3年前)。谢谢!请务必添加答案,以便我将其标记为已解决。
标签: objective-c nsarray nspredicate kvc