如何使用swift检查核心数据是否为空.我试过这个方法:
var people = [NSManagedObject]()
if people == nil {
}
Run Code Online (Sandbox Code Playgroud)
但这会导致此错误
"binary operator'=='不能应用于[NSManagedObject]和nil类型的操作数"
编辑1 虽然我理解对于这个特定场景(和其他类似的场景)我可以单独使用映射编辑器来正确迁移我的存储,以便持久存储中的值不会跳转,但这不是我当前问题的解决方案但只能避免解决问题的根源.我热衷于坚持使用自定义迁移策略,因为这样可以让我在迁移过程中获得很多控制权,特别是对于未来的设置自定义迁移策略对我来说很有意义.这是一个长期的解决方案,而不仅仅是这种情况.
我恳请您尝试帮我解决当前的情况,而不是将我转移到轻量级迁移或建议我避免使用迁移策略.谢谢.
我真的很期待把这个问题整理出来,以及我对解决这个问题我能做些什么的宝贵意见.
我做了什么:
我设置了迁移策略,以便可以将源数据复制到version 1核心模型的目标数据中version 2.
这是迁移政策:
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error {
// Create the product managed object
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName]
inManagedObjectContext:[manager destinationContext]];
NSString *productCode = [sInstance valueForKey:@"productCode"];
NSNumber *productPrice = [sInstance valueForKey:@"productPrice"];
[newObject setValue:productCode forKey:@"productCode"];
[newObject setValue:productPrice forKey:@"productPrice"];
//This is the field where the name has changed as well as the type.
[newObject setValue:[NSNumber numberWithBool:YES] forKey:@"productPriceNeedsUpdating"];
// Set up the association between the old source …Run Code Online (Sandbox Code Playgroud)