我正在尝试为CoreData条目执行自定义迁移。我需要更改属性的名称和类型。
所以我添加了新的datamodel和mappingmodel。例如,我在旧模型中有myData(String),我在新模型中需要CarDate(Date)。 我添加了行:CarDate => $ source.myData到* .xcmappingmodel文件并创建了类:
@implementation RemindersMigration
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance
entityMapping:(NSEntityMapping *)mapping
manager:(NSMigrationManager *)manager
error:(NSError **)error {
NSManagedObject *newObject =
[NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName]
inManagedObjectContext:[manager destinationContext]];
NSString *dateString = [sInstance valueForKey:@"myData"];
NSDateFormatter *formatter = ...;
NSDate *date = [formatter dateFromString:dateString];
[newObject setValue:date forKey:@"CarDate"];
[manager associateSourceInstance:sInstance withDestinationInstance:newObject forEntityMapping:mapping];
return YES;
}
我用模拟器测试了迁移,所以我用Git切换到旧版本,添加了新条目,切换到了新版本。在CarDate我没有。 请指出我错在哪里?