OGeek|极客世界-中国程序员成长平台

标题: ios - 在 CoreData 实体中将属性从 NSNumber 转换为 NSString - LightWeightMigration [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:04
标题: ios - 在 CoreData 实体中将属性从 NSNumber 转换为 NSString - LightWeightMigration

我正在尝试使用 Objective-C 中的轻量级迁移将 NSNumber 迁移到 NSString 属性。版本 0 enter image description here

第 2 版

enter image description here

我已将核心数据模型当前版本更改为 2

enter image description here

由于它不会在轻量级迁移中得到解决,我尝试使用 CoreDataMapping 模型和 NSEntity 迁移策略,如下所示。 enter image description here

创建了 NSEntityMigrationPolicy 的子类并尝试了以下代码

@interface SampleMigrationV1toV2 : NSEntityMigrationPolicy

@end

#import "SampleMigrationV1toV2.h"

@implementation SampleMigrationV1toV2

- (BOOL)createDestinationInstancesForSourceInstanceNSManagedObject *)sInstance entityMappingNSEntityMapping *)mapping managerNSMigrationManager *)manager errorNSError **)error
{
    NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]];

    // Copy all the values from sInstance into newObject, making sure to apply the conversion for the string to int when appropriate. So you should have one of these for each attribute:
    [newObject setValue:[sInstance valueForKey"number"] forKey"number"];

    [manager associateSourceInstance:sInstance withDestinationInstance:newObject forEntityMapping:mapping];
    return YES;
}
@end

在持久协调器中,我已将 NSInferMappingModelAutomaticallyOption 更改为 NO,如下所示,

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent"CoreDataMigrationPolicty.sqlite"];

    NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

    NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setObject:[NSNumber numberWithBool:NO] forKey:NSInferMappingModelAutomaticallyOption];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl optionsptions error:&error])
    {
        //NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return persistentStoreCoordinator;
}

做完所有这些事情后,我收到了类似“Error Domain=NSCocoaErrorDomain Code=134110”(null)"UserInfo={NSUnderlyingException=Couldn't create mapping policy for class named (CoreDataMigrationPolicty.SampleMigrationV1toV2)} with userInfo 字典之类的错误

未创建/调用映射模型。更改核心数据中的属性数据类型是否正确?有没有其他方法可以在不崩溃/重新安装应用程序的情况下解决此问题。



Best Answer-推荐答案


最后我发现我从这个答案中所做的错误。 Core Data Migration: Attribute Mapping Value Expression

问题在于我在映射模型属性中提到的表达式。 语法是 FUNCTION($entityPolicy, "convertNumberToString:", $source.number) 函数定义是这样的

#import "SampleMigrationV1toV2.h"

@implementation SampleMigrationV1toV2

-(NSString *)convertNumberToStringNSNumber *)number{
    return  [number stringValue];
}
@end

Mention Custom policy name(i.e.SampleMigrationV1toV2) on clicking Entity Mapping SampleToSample in

Enter the expression "FUNCTION($entityPolicy, "convertNumberToString:" , $source.number)" on clicking the attribute you want to migrate

FUNCTION($entityPolicy, "convertNumberToString:", $source.number) :

$entityPolicy 获取我们在EntityMapping“SampleToSample”中提到的对应的MappingPolicy(即SampleMigrationV1toV2)和函数“convertNumberToString:”通过取输入值$source.Number将数字迁移到字符串

关于ios - 在 CoreData 实体中将属性从 NSNumber 转换为 NSString - LightWeightMigration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46892791/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4