2014-03-03 149 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