我有一个具有两个关系的客户对象:一个文档和一个 Assets 。
当我创建 Assets 时,我有一个条件,即检查客户中是否存在文档对象。如果它不存在,我将创建一个并设置文档的客户关系。
之后,我创建了 Assets 。但是,当我尝试使用 EXC_BAD_ACCESS 将客户设置为 Assets 时,我遇到了崩溃。
奇怪的是这段代码可以正常工作,但我不知道会发生什么变化会突然导致这里崩溃。
这是代码:
if (![customer inspectionDocument]) //check if a document exists
{
// if it doesn't create one
Document *document = [Document newDocument];
document.customer = customer;
//sets additional properties
}
// now create the asset
Asset *asset = [Asset newObject];
asset.customer = customer; // <---- crash occurs here
//sets additional properties
最佳答案
我假设您正在使用关系来存储对您的文档和 Assets 的客户引用。通常,你们应该有相反的关系。您是否尝试过在客户对象上设置文档属性而不是相反?
if (![customer inspectionDocument]) {
// if it doesn't create one
customer.document = [Document newDocument];
}
customer.asset = [Asset newObject];
关于ios - 第二次设置核心数据对象会导致 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18360862/