Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data setPrimitive-setter causes EXC_BAD_ACCESS

when I try to set the variable eventId of my NSManagedObject (Event) I get ther error EXC_BAD_ACCESS. I don't know the reason.

Here's the code of my class Event

@interface Event : NSManagedObject 

@property (assign)              NSInteger   eventId;

@end


@interface Event (PrimitiveAccessors)
- (NSInteger)primitiveEventId;
- (void)setPrimitiveEventId:(NSInteger)event_id;
@end



@implementation Event
@dynamic eventId;

...

-(NSInteger)eventId
{
    [self willAccessValueForKey:@"eventId"];
    NSInteger id = [self eventId];
    [self didAccessValueForKey:@"eventId"];
    return id;
}

-(void)setEventId:(NSInteger)event_id
{
    [self willChangeValueForKey:@"eventId"];
    [self setPrimitiveEventId:event_id]; //Here I get the error
    [self didChangeValueForKey:@"eventId"];
}

...

Anyone can solve the problem?

Thanks for help

like image 355
Tobi Weißhaar Avatar asked Dec 03 '25 15:12

Tobi Weißhaar


1 Answers

If you look closely at the documentation, you'll see that the primitive accessor methods for the scalar attribute double length in the example still use NSNumber * arguments and return values:

@interface MyManagedObject (PrimitiveAccessors)
@property (nonatomic, retain) NSNumber *primitiveLength;
@end

[Note: I added the * that was missing in the original code snippet in Apple's documentation.]

So try declaring your primitive accessors with NSNumber * arguments and return values.

Or if you really must, consider implementing your own primitive accessor methods.

like image 82
puzzle Avatar answered Dec 06 '25 04:12

puzzle