我正在尝试像这样为“核心数据实体”保存值:
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
NSError *error = nil;
for (NSDictionary *dic in controlTypeResponse.DataDic) {
ControlType *controlType = [NSEntityDescription insertNewObjectForEntityForName:@"ControlType" inManagedObjectContext:coreDataStack.managedObjectContext];
controlType.controlTypeID = [[dic valueForKey:@"ControlTypeID"] integerValue];
controlType.controlTypeName = [dic valueForKey:@"ControlTypeName"];
}
[coreDataStack.managedObjectContext save:&error];
但是,当分配controlTypeID
时,应用会崩溃,并显示EXC_BAD_ACCESS
错误。有趣的是,错误详细信息(code=1, address=0x2)
中的内存地址字段根据[[dic valueForKey:@"ControlTypeID"] integerValue]
的值而变化(例如,在这种情况下,值为2)。
是否知道为什么会这样?我在SO上搜索了核心数据文档和与并发相关的问题,但是我的问题似乎与这些问题无关。
答案 0 :(得分:0)
可以肯定的是,您在此处创建的类型不匹配...
那么在您的Core Data模型中,您是否将Class Codegen设置为“ Class Definition”或“ Manual / None”?
如果将其设置为“手动/无”,则可以检查NSManagedObject
实体的ControlType
子类头文件。
也许属性的声明看起来像这样?
@property (nonatomic, retain) NSNumber * controlTypeID;
如果没有,它将设置为什么类型?
如果将其设置为“类定义”,则将无法检查实体NSManagedObject
的{{1}}子类,但是我知道Core Data会默认所有Integer(Integer) 16,32,64)作为类型ControlType
。
因此,为使设置NSNumber
起作用的调用,您需要将属性controlType.controlTypeID
的数字值转换为Core Data可以转换为模型的类型(并且从而持久存储到持久存储中。
如果我的猜测是正确的,这可能会起作用...
controlTypeID
如果没有,也许我误会了...让我知道。