- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 Objective-C 中的轻量级迁移将 NSNumber 迁移到 NSString 属性。版本 0
版本 2
我已经将核心数据模型的当前版本更改为 2
由于它不会在轻量级迁移中解决,我尝试使用 CoreDataMapping 模型和 NSEntity 迁移策略,如下所示。
已经创建了 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
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/
我正在开发一些代码,其中我使用扫描仪从字符串中获取 NSNumbers,例如 x 和 y。 现在我想从 x 和 y 计算一些简单的东西,比如 z = 10.0/(x + y/60.0)/60.0)。我
第一次发帖。 NSNumber 的 floatValue 方法有问题——不知何故,它返回一个不精确的数字。问题在于:我将一堆 NSNumbers 存储在一个数组中,如下所示: NSArray *a =
我在尝试使用从基类继承的泛型函数的参数时遇到标题错误 基类: BaseBottomBar: UIView { ... func formatDetailText(value: T...
我有下面这行代码 NSNumber *myValue = loadTempValue*0.420; 我试图将 *myValue 的值设置为 loadTempValue*0.420 的值, 但是,我得到
我在将 NSNumber 转换为字符串以及将字符串转换为 NSNumber 时遇到问题。 这是一个示例问题: NSString *stringValue = @"9.2"; NSNumberForma
有一个坐标对象,具有三个变量纬度(NSNumber),经度(NSNumber)和时间(NSDate),为了在我的模拟器上检查程序,我给出了以下代码 [coordinate setLatitude:[N
简单方法:我有一个 SearchView,用户可以在其中选择一些 CoreDate,然后他可以搜索 Name、City( >NSString) 和Clientnumber (NSNumber) NSP
我转换这个 Objective-C 函数: - (NSArray *)grt_map:(id (^)(id))block { NSMutableArray *newArray = [NSMut
示例:我有一个 NSInteger,我将其包装到一个 NSNumber 对象中。现在我想要一个具有该 NSInteger 值的 NSDecimal。 那么我可以问: NSDecimal myDecim
我有这个自定义类: @interface MyModel : NSObject @property (nonatomic,strong) NSString *id_name; @property (n
在我的应用程序中,我进入 CoreData 并获取一个类型为 Double 的条目,然后我试图将该值放入我的应用程序的文本字段中。 这看起来像 lengthTextField.text = lastS
用户输入一个数字( float 或整数),并且它必须大于下限。 这是从 UITextField 获取数字的代码: NSNumberFormatter * f = [[NSNumberFormatter
我正在为 iPhone 应用程序使用 NSNumber,并看看我能用它做什么。对于我的大多数变量,我简单地将它们存储为“int”或“float”或其他类型。但是,当我必须传递一个对象(例如在字典中)时
我使用 [NSNumber numberWithDouble:] 方法来形成我的 NSNumber。但是,由于某些值可能很大,我想知道是否有办法告诉 NSNumber 用科学记数法表示自己。提前致谢!
一个 NSNumber 可以存储不同的基本类型,如short、int、long、long long、float、double 但是当我这样做时,尺寸会改变 @(long long) 相比 @(int)
我编写了一个 Objective-C 类,并且在我的 iPhone 项目中的多个 View 中使用它的共享实例。它的成员变量包括 bools、ints、NSStrings 和一个 NSNumber。共
创建一个 ARC 新项目并将此代码注入(inject)到 didFinishLaunchingWithOptions 中。 for (int i=0; i < 1000000; i++) {
我对 Objective C 比较陌生,需要一些数组帮助。 我有一个 plist,其中包含一个字典和一个 NSNumber 数组,还有更多数组可供使用稍后添加。 NSMutableDictionary
我正在尝试将 NSNumber 放入数组中: NSNumber *n = [NSNumber numberWithInt:1]; [[array objectAtIndex:0] setValue:n
我正在开发一个简单的计算器 iPhone 应用程序。只是为了练习的目的。我有一个 IBAction 方法来存储用户输入的数字。整个概念是计算器应用程序堆积待处理的操作,以便用户可以执行多个操作,并且屏
我是一名优秀的程序员,十分优秀!