Ios 在核心数据中执行重载迁移时尝试迁移nil属性值
在我的iOS应用程序中,我正在对一个实体进行重量迁移,将属性的类型从旧数据模型中的Integer64转换为新数据模型中的字符串类型。此属性的转换似乎工作正常。但是,我遇到的问题是,同一实体的另一个属性为null(这是应该的),当该属性迁移到新架构中的同一实体时,会标记一个错误:Ios 在核心数据中执行重载迁移时尝试迁移nil属性值,ios,core-data,Ios,Core Data,在我的iOS应用程序中,我正在对一个实体进行重量迁移,将属性的类型从旧数据模型中的Integer64转换为新数据模型中的字符串类型。此属性的转换似乎工作正常。但是,我遇到的问题是,同一实体的另一个属性为null(这是应该的),当该属性迁移到新架构中的同一实体时,会标记一个错误: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of valu
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "date"; desired type = NSDate; given type = NSNull; value = <null>.'
有人知道我做错了什么吗?问题是您在字典中得到了一个
NSNull
类,这意味着您试图将错误类型的类传递给新的NSManagedObject
实例
如果您阅读了-committedValuesForKeys:
上的文档,您将看到:
nil
值由NSNull
的实例表示
这是你的问题
就我个人而言,我不会以这种方式对待这些价值观。相反,我会这样做:
NSDictionary *allAttributes = [[sInstance entity] attributesByName];
for (NString *key in allAttributes) {
id value = [sInstance valueForKey:key];
if ([key isEqualToString:@"integerType"]) {
//here retrieve old value
NSNumber *oldValue = [keyValDict objectForKey:key];
//here do conversion as needed
NSString *stringType = [oldValue stringValue];
//then store new value
[newObject setValue:stringType forKey:key];
} else { //no need to modify the value, Copy it across -- this is where I believe the problem is
[newObject setValue:value forKey:key];
}
}
这样,您就可以直接从对象中获取值,并获得正确的
nil
。非常感谢您的解决方案,它成功了!如果您有时间,请检查下一个问题:?再次感谢您的时间和帮助!
NSDictionary *allAttributes = [[sInstance entity] attributesByName];
for (NString *key in allAttributes) {
id value = [sInstance valueForKey:key];
if ([key isEqualToString:@"integerType"]) {
//here retrieve old value
NSNumber *oldValue = [keyValDict objectForKey:key];
//here do conversion as needed
NSString *stringType = [oldValue stringValue];
//then store new value
[newObject setValue:stringType forKey:key];
} else { //no need to modify the value, Copy it across -- this is where I believe the problem is
[newObject setValue:value forKey:key];
}
}