Question

H file:

@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 file:

@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

When setting the property, It doesn't work:

TaskTypeEntity *taskTypeEntity = [TaskTypeEntity alloc];
taskTypeEntity.name = @"dfdfd";
[taskTypeModel add:taskTypeEntity];

error: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TaskTypeEntity setName:]: unrecognized selector sent to instance 0x8a7b070'

Please help me, thank you

Was it helpful?

Solution

According to the NSManagedObject class reference:

Important: This method is the designated initializer for NSManagedObject. You must not initialize a managed object simply by sending it init.

The method of reference in the above quoted text is insertNewObjectForEntityForName:inManagedObjectContext: and can be used thusly:

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

EDIT (In response...)

This code is just plain wrong:

TaskTypeEntity *taskTypeEntity = [TaskTypeEntity alloc];

Even if TaskTypeEntity were not a NSManagedObject it would still be wrong, as you never called an initializer.

The fact that it is a NSManagedObject makes it even more wrong, because you are never supposed to alloc/init one of those.

Why not try something like this (I assume you are using transformable attributes for image and color):

+ (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;
}

Then you can call it...

TaskTypeEntity *taskTypeEntity =
    [TaskTypeEntity taskTypeEntityInMOC:context
                                   name:(NSString*)name
                                  image:(UIImage*)image
                                  color:(UIColor*)color];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top