EXC_BAD_ACCESS when setting Integer to anything else than zero

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 02:17:27

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:

//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 regenerate the class via the Create NSManagedObject Subclass template, making sure that Use Scalar Values for Primitives was checked.

This issue happened to me because the entity wasn't correctly linked to my class files. When this happened, Core Data creates its own subclass dynamically, rather than using my own, and that subclass's setters expect boxed primitives.

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

When you use XCode's Create NSManagedObject Subclass template, XCode will automatically set this value for the corresponding entity in the model file. This is probably what fixed borrrden's problem. However, you should be able to achieve the same result without creating new files by ensuring this value is set correctly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!