核心数据集属性

时间:2010-10-10 12:35:22

标签: objective-c cocoa core-data

我正在尝试使用Core Data设置属性,但它不起作用。每当我调用方法setAveScore时,它都会抛出错误[Stats setAveScore:]: unrecognized selector sent to instance 0x3364c0

知道出了什么问题吗?

电话:

Stats *sObj = [Stats alloc];
NSNumber *foo = [[NSNumber alloc ]initWithInt:1];
sObj.aveScore = foo;

核心数据类:

@interface Stats :  NSManagedObject  
{
}

@property (nonatomic, retain) NSNumber * aveScore;
@end

#import "Stats.h"


@implementation Stats 
@dynamic aveScore;
@end

整个错误消息:

-[Stats setAveScore:]: unrecognized selector sent to instance 0x1494b0
Exception detected while handling key input.
-[Stats setAveScore:]: unrecognized selector sent to instance 0x1494b0

1 个答案:

答案 0 :(得分:1)

sObj未正确初始化。要获取Core Data托管对象,必须将其插入到托管对象上下文中,如下所示:

NSManagedObjectContext context = ...
Stats *sObj = [NSEntityDescription insertNewObjectForEntityForName:@"Stats" inManagedObjectContext:context];

假设您的实体名称是@“Stats”。