Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用RKValueTransformer将NSString转换为核心数据NSManagedObject_Ios_Core Data_Restkit - Fatal编程技术网

Ios 如何使用RKValueTransformer将NSString转换为核心数据NSManagedObject

Ios 如何使用RKValueTransformer将NSString转换为核心数据NSManagedObject,ios,core-data,restkit,Ios,Core Data,Restkit,我正在从服务器获取一个字符串,我需要将其转换为一个状态对象,它是NSManagedObject的子类。转换器工作正常,只是在映射时我不知道ManagedObjectContext是什么,所以我在尝试在不同上下文中的对象之间建立关系“状态”时出错 RKEntityMapping知道如何在映射操作期间自动创建适当的对象,因此该功能在RestKit中。如何正确创建此核心数据对象 RKValueTransformer *stateTransformer = [RKBlockValueTransforme

我正在从服务器获取一个字符串,我需要将其转换为一个状态对象,它是NSManagedObject的子类。转换器工作正常,只是在映射时我不知道ManagedObjectContext是什么,所以我在尝试在不同上下文中的对象之间建立关系“状态”时出错

RKEntityMapping知道如何在映射操作期间自动创建适当的对象,因此该功能在RestKit中。如何正确创建此核心数据对象

RKValueTransformer *stateTransformer = [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class sourceClass, __unsafe_unretained Class destinationClass) {
    return ([sourceClass isSubclassOfClass:[NSString class]] && [destinationClass isSubclassOfClass:[State class]]);
} transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, Class outputValueClass, NSError *__autoreleasing *error) {
    // Validate the input and output
    RKValueTransformerTestInputValueIsKindOfClass(inputValue, [NSString class], error);
    RKValueTransformerTestOutputValueClassIsSubclassOfClass(outputValueClass, [State class], error);

    State *state = [NSEntityDescription insertNewObjectForEntityForName:@"State" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];

    state.abbreviation = inputValue;

    *outputValue = state;

    return YES;
}];

看起来您正在创建状态对象,并将字符串设置为对象上的单个属性。要做到这一点,您不需要自定义转换器,只需要使用nil键路径映射

请参见的“无关键路径的映射值”部分