2014-03-03 62 views
0

.h文件:@dynamic如何设置属性?

@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”,重新ASON: ' - [TaskTypeEntity的setName:]:无法识别的选择发送到实例0x8a7b070'

请帮帮我,谢谢

+0

这不是办法创建实体(尽管它不是创建任何Objective-C对象的方式)。请阅读Apple的核心数据指南。 – Desdenova

+0

[核心数据编程指南介绍](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/cdProgrammingGuide.html) – Desdenova

+0

谢谢,我的英文不是很好,但我会仔细阅读 – user3374273

回答

0

按照NSManagedObject类引用:

重要提示:此方法是指定的初始值为 NSManagedObject。您不能简单地通过 发送init来初始化管理对象。

在上述引用的文本参考的方法是insertNewObjectForEntityForName:inManagedObjectContext:并且可以正是如此使用:

NSManagedObject *object = [NSEntityDescription 
    insertNewObjectForEntityForName:@"Item" 
      inManagedObjectContext:context]; 

EDIT(响应...)

此代码是完全错误的:

TaskTypeEntity *taskTypeEntity = [TaskTypeEntity alloc]; 

即使TaskTypeEntity没有它仍然是错误的,因为你从来没有称过初始值设定项。

事实上,它是一个NSManagedObject使它更加错误,因为你永远不应该分配/初始化其中的一个。

为什么不尝试这样的事情(我假设你正在使用的图像和颜色变形属性):

+ (instancetype)taskTypeEntityInMOC:(NSManagedObjectContext*)context 
           name:(NSString*)name 
           image:(UIImage*)image 
           color:(UIColor*)color 
{ 
    TaskTypeEntity *taskTypeEntity = [NSEntityDescription insertNewObjectForEntityForName:ENTITY_NAME inManagedObjectContext:content]; 
    taskTypeEntity.name = name; 
    taskTypeEntity.image = image; 
    taskTypeEntity.color = color; 
    return taskTypeEntity; 
} 

然后就可以调用它...

TaskTypeEntity *taskTypeEntity = 
    [TaskTypeEntity taskTypeEntityInMOC:context 
            name:(NSString*)name 
            image:(UIImage*)image 
            color:(UIColor*)color]; 
+0

我知道,但我只是想被包装成一个函数,并且方便调用。像这样: – user3374273

+0

- (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 = [content save:nil];如果(结果){ NSLog(@“success%@”,data);其他{ }其他{ NSLog(@“fail”); } } else { NSLog(@“fail”); } } – user3374273

+0

我修改了问题 – user3374273