EXC_BAD_ACCESS when installing Integer on anything but zero

I have this object:

@interface Song : NSManagedObject @property (nonatomic, strong) NSString *songName; @property (nonatomic) int32_t achievedPoints; 

When I set the properties as follows

 Song *song1 = [[SongStore sharedStore] createSong]; song1.songName = @"Song 1"; song1.achievedPoints = 0; 

everything works, however as soon as I try to set the achievedPoints variable to something other than 0, I get EXC_BAD_ACCESS.

This is what the createSong method createSong :

 - (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 nothing but zero, the program crashes. Any help is appreciated.

+1
source share
2 answers

This has happened to me before. I donโ€™t know exactly which settings are confusing, but it seems to me that your model of managed objects has a parameter that controls whether the class should use primitive values โ€‹โ€‹(int, float, etc.) or object values โ€‹โ€‹(NSNumber *, and etc.) to set its values. If this is confusing, and you think that you are installing the primitive, when you actually install the object, the following will happen:

 //song1.achievedPoints = 0; song1.achievedPoints = (NSNumber *)0x00000000; //This is OK because it is the same as nil //song1.achievedPoints = 1; song1.achievedPoints = (NSNumber *)0x00000001; //You can see why this is bad! 

My solution was to restore the class using the Create NSManagedObject Subclass template , making sure that Use Scalar Values โ€‹โ€‹for Primitives was checked.

+4
source

This problem happened to me because the object was not properly associated with my class files. When this happened, Core Data creates its own subclass dynamically, instead of using my own, and that subclass installers expect boxed primitives.

You can fix this by making sure that the "Class" property of your object in the model file is set to the correct class, and not the default NSManagedObject .

Object Configuration

When using the Xcode template Create NSManagedObject Subclass, Xcode will automatically set this value for the corresponding object in the model file. This is probably a fixed problem borrrden. However, you should be able to achieve the same result without creating new files, ensuring that this value is set correctly.

0
source

Source: https://habr.com/ru/post/1488580/


All Articles