mriddi
mriddi

Reputation: 423

ManagedObject custom class init error

I have my custom Managed Object class:

@interface NDAEntity : NSManagedObject

@property (nonatomic, retain) NSString * text;

+(id) entityWithText:(NSString *)text;
-(void) setText:(NSString *)text;
- (void)setPrimitiveName:(NSString *)text;

@end

@implementation NDAEntity

@dynamic text;

+(id) entityWithText:(NSString *)text{
    return [[NDAEntity alloc] initWithText:text];
}

-(id) initWithText:(NSString *)text{
    if(self = [super init]){
        [self setText:text];
    }
    return self;
}

-(void) setText:(NSString *)text{
    [self willChangeValueForKey:@"text"];
    [self setPrimitiveName:text];
    [self didChangeValueForKey:@"text"];
}

@end

And when I push button execute next code:

NDAEntity *entity = [NDAEntity entityWithText: @"smth"];

I have error:

CoreData: error: Failed to call designated initializer on NSManagedObject class 'NDAEntity'

How can I solve this?

Upvotes: 0

Views: 184

Answers (1)

Wain
Wain

Reputation: 119031

You can't just alloc init a managed object subclass. You must create the managed object instance from the entity and with reference to a context.

Generally you should be using insertNewObjectForEntityForName:inManagedObjectContext: to do this.

Upvotes: 1

Related Questions