I have this object:
@interface Song : NSManagedObject
@property (nonatomic, strong) NSString *songName;
@property (nonatomic) int32_t achievedPoints;
When I set the properties like this
Song *song1 = [[SongStore sharedStore] createSong];
song1.songName = @"Song 1";
song1.achievedPoints = 0;
everything works, however once I try to set the achievedPoints variable to something else than 0 I get an EXC_BAD_ACCESS.
This is what the createSong method does:
- (Song *)createSong {
double order;
if ([allSongs count] == 0) {
order = 1.0;
} else {
order = [[allSongs lastObject] orderingValue] + 1.0;
}
Song *p = [NSEntityDescription insertNewObjectForEntityForName:@"Song" inManagedObjectContext:context];
[p setOrderingValue:order];
[allSongs addObject:p];
return p;
}
I have no idea why getting the value and setting it to 0 works but anything else than zero crashes the program. Any help is highly appreciated.
This has happened to me once before. I don’t know exactly which settings gets messed up, but there is a setting in your managed object model, I think, that controls whether or not the class should use primitive values (int, float, etc) or object values (NSNumber *, etc) to set its values. If this gets messed up, and you think you are setting a primitive when you are actually setting an object the following will happen:
My solution was to regenerate the class via the Create NSManagedObject Subclass template, making sure that Use Scalar Values for Primitives was checked.