gpt4 book ai didi

ios - 将属性从 NSNumber 转换为 CoreData 实体中的 NSString - LightWeightMigration

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:59 25 4
gpt4 key购买 nike

我正在尝试使用 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)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)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 options:options 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 dictionary 这样的错误

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

最佳答案

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

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

#import "SampleMigrationV1toV2.h"

@implementation SampleMigrationV1toV2

-(NSString *)convertNumberToString:(NSNumber *)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"和函数 "convertNumberToString:"中提到的相应的 MappingPolicy(即 SampleMigrationV1toV2),通过获取输入值 $source.Number 将数字迁移为字符串

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com