将Integer设置为非零值时,指定EXC_BAD_ACCESS [英] EXC_BAD_ACCESS when setting Integer to anything else than zero

查看:155
本文介绍了将Integer设置为非零值时,指定EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个对象:

@interface Song : NSManagedObject

@property (nonatomic, strong) NSString *songName;
@property (nonatomic) int32_t achievedPoints;

当我设置像这样的属性

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

一切正常,但是一旦我尝试设置 achiePoints variable = 0我得到一个EXC_BAD_ACCESS。

everything works, however once I try to set the achievedPoints variable to something else than 0 I get an EXC_BAD_ACCESS.

这是 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;
}

我不知道为什么获取该值并将其设置为0,否则会导致程序崩溃。任何帮助是非常感激。

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.

推荐答案

这发生在我以前。我不知道确切的哪些设置被搞砸了,但是在你的托管对象模型中有一个设置,控制是否该类应该使用原始值(int,float等)或对象值(NSNumber * ,等)来设置其值。如果这被搞砸了,你认为你正在设置一个原始,当你实际设置一个对象,将发生以下情况:

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!



我的解决方案是通过创建NSManagedObject子类模板重新生成类,确保已选中使用标量值作为基本元素

My solution was to regenerate the class via the Create NSManagedObject Subclass template, making sure that Use Scalar Values for Primitives was checked.

这篇关于将Integer设置为非零值时,指定EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆