Core data @动态如何设置属性?
H文件:Core data @动态如何设置属性?,core-data,dynamic,Core Data,Dynamic,H文件: @interface TaskTypeEntity : NSManagedObject @property (nonatomic, retain) UIColor *color; @property (nonatomic, retain) UIImage *image; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSNumber * status; @property (n
@interface TaskTypeEntity : NSManagedObject
@property (nonatomic, retain) UIColor *color;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * status;
@property (nonatomic, retain) NSSet *task;
@property (nonatomic, retain) NSSet *taskCount;
@end
M文件:
@implementation TaskTypeEntity
@dynamic color;
@dynamic image;
@dynamic name;
@dynamic status;
@dynamic task;
@dynamic taskCount;
- (void) add:(TaskTypeEntity*)data
{
TaskTypeEntity *taskTypeEntity = [NSEntityDescription insertNewObjectForEntityForName:ENTITY_NAME inManagedObjectContext:content];
taskTypeEntity.name = data.name;
taskTypeEntity.image = data.image;
taskTypeEntity.color = data.color;
BOOL result = [content save:nil];
if (result) {
NSLog(@"success%@", data);
}else{
NSLog(@"fail");
}
}
@end
设置属性时,它不起作用:
TaskTypeEntity *taskTypeEntity = [TaskTypeEntity alloc];
taskTypeEntity.name = @"dfdfd";
[taskTypeModel add:taskTypeEntity];
错误:
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[TaskTypeEntity setName:]:发送到实例0x8a7b070的选择器无法识别”
请帮助我,谢谢根据
NSManagedObject
课程参考:
重要提示:此方法是的指定初始值设定项
NSManagedObject。不能仅通过以下操作初始化托管对象:
将其发送到init
上述引用文本中的引用方法为insertNewObjectForEntityForName:inManagedObjectContext:
,可用于:
NSManagedObject *object = [NSEntityDescription
insertNewObjectForEntityForName:@"Item"
inManagedObjectContext:context];
编辑(响应…)
此代码完全错误:
TaskTypeEntity *taskTypeEntity = [TaskTypeEntity alloc];
即使TaskTypeEntity
不是NSManagedObject
它仍然是错误的,因为您从未调用过初始值设定项
事实上,它是一个NSManagedObject
使得它更加错误,因为您永远不应该将其中一个配置为alloc/init
为什么不试试这样的方法(我假设您正在使用图像和颜色的可转换属性):
那你可以叫它
TaskTypeEntity *taskTypeEntity =
[TaskTypeEntity taskTypeEntityInMOC:context
name:(NSString*)name
image:(UIImage*)image
color:(UIColor*)color];
它不是创建实体的方法(尽管它不是创建任何Objective-C对象的方法)。请阅读苹果的核心数据指南。谢谢,我的英语不是很好,但我会仔细阅读。我知道,但我只想被包装成一个函数,方便调用。如下:-(void)add:(TaskTypeEntity*)data{TaskTypeEntity*TaskTypeEntity=[NSEntityDescription insertNewObjectForEntityForName:ENTITY_NAME inManagedObjectContext:content];taskTypeEntity.NAME=data.NAME;taskTypeEntity.image=data.image;taskTypeEntity.color=data.color;//taskType.status=data.status;if(taskTypeEntity!=nil){BOOL result=[内容保存:nil];if(结果){NSLog(@“success%@”,data);}else{NSLog(@“fail”);}else{NSLog(@“fail”);}如果没有更好的方法,我只以这种方式修改问题谢谢!!非常感谢。
TaskTypeEntity *taskTypeEntity =
[TaskTypeEntity taskTypeEntityInMOC:context
name:(NSString*)name
image:(UIImage*)image
color:(UIColor*)color];