我正在尝试像这样保存核心数据实体的值:
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上搜索了核心数据文档和与并发相关的问题,但是我的问题似乎与这些问题无关。
当然可以确定您在此处创建类型不匹配...
因此,在您的Core Data模型中,您是否将Class Codegen设置为“ Class Definition”或“ Manual / None”?
如果将其设置为“手动/无”,则可以检查实体NSManagedObject
的ControlType
子类。
查看属性controlTypeID
的类型...我怀疑它是NSNumber
类型?如果不是,它将设置为什么类型?
如果将其设置为“类定义”,则将无法检查实体NSManagedObject
的ControlType
子类,但是我知道Core Data会默认所有整数(整数16、32、64 )设为NSNumber
。
因此,要使设置controlType.controlTypeID
的调用正常工作,您需要将属性controlTypeID
的数字值转换为Core Data可以转换为模型的类型(从而持久化持久性商店)。
如果我的猜测是正确的,这可能会起作用...
controlType.controlTypeID = [NSNumber numberWithInt:[dic valueForKey:@"ControlTypeID"]]
如果没有,也许我误会了...让我知道。