0

我正在使用 RestKit v0.21 并尝试使用动态名称映射一组值。我能够正确获取自定义字段的名称,但无法捕获关联的值。JSON 看起来像这样:

{
    "id": 1,
    "firstName": "Kenny",
    "lastName": "Powers",
    "customFields": {
        "favorite color": "blue",
        "hometown": "Cleveland",
        "spouse name": "sally"
    }
}

我的映射如下所示:

//PERSON MAPPING
RKEntityMapping *personMapping = [RKEntityMapping mappingForEntityForName:@"Person" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[personMapping addAttributeMappingsFromDictionary:@{
                                                     @"id":             @"personId",
                                                     @"firstName":      @"firstName",
                                                     @"lastName":       @"lastName"}];
personMapping.identificationAttributes = @[ @"personId" ];

//CUSTOM FIELD MAPPING
RKEntityMapping *customFieldMapping = [RKEntityMapping mappingForEntityForName:@"CustomValue" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
customFieldMapping.forceCollectionMapping = YES;
[customFieldMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"fieldName"];
[customFieldMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"(fieldName)" toKeyPath:@"fieldValue"]];

[personMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"customFields"
                                                                             toKeyPath:@"customValues"
                                                                           withMapping:customFieldMapping]];

我在动态属性中看到的所有示例都涉及值对象,其中值映射类似于“(fileName).email”。在我的情况下,它始终只是一个字符串:字符串名称和值的集合,它们代表一组完全动态的自定义字段和随附的值。

当我检查自定义字段对象的集合时,设置了 fieldName 属性,但 fieldValue 属性都是(null)。

有任何想法吗?

更新:这是自定义字段数组中元素的映射之一的日志输出:

2013-10-11 09:54:45.558 MyMobile[45460:6207] D restkit.object_mapping:RKMappingOperation.m:851 Starting mapping operation...
2013-10-11 09:54:45.558 MyMobile[45460:6207] T restkit.object_mapping:RKMappingOperation.m:852 Performing mapping operation: <RKMappingOperation 0x17a71230> for 'CustomValue' object. Mapping values from object {
    "Youtube Link" = "http://www.youtube.com";
} to object <CustomValue: 0xac42b00> (entity: CustomValue; id: 0xac69420 <x-coredata://D54F8070-D653-49E2-AFD5-90CD9778B2D4/CustomValue/p3> ; data: {
    fieldName = "Youtube Link";
    fieldValue = nil;
    person = "0x16f8d200 <x-coredata://D54F8070-D653-49E2-AFD5-90CD9778B2D4/Person/p389>";
}) with object mapping (null)
2013-10-11 09:54:45.559 MyMobile[45460:6207] D restkit.object_mapping:RKMappingOperation.m:813 Found nested mapping definition to attribute 'fieldName'
2013-10-11 09:54:45.560 MyMobile[45460:6207] D restkit.object_mapping:RKMappingOperation.m:816 Found nesting value of 'Youtube Link' for attribute 'fieldName'
2013-10-11 09:54:45.562 MyMobile[45460:6207] T restkit.object_mapping:RKMappingOperation.m:440 Mapping attribute value keyPath '<RK_NESTING_ATTRIBUTE>' to 'fieldName'
2013-10-11 09:54:45.562 MyMobile[45460:6207] T restkit.object_mapping:RKMappingOperation.m:429 Found transformable value at keyPath '<RK_NESTING_ATTRIBUTE>'. Transforming from class '__NSCFString' to 'NSString'
2013-10-11 09:54:45.563 MyMobile[45460:6207] T restkit.object_mapping:RKMappingOperation.m:475 Skipped mapping of attribute value from keyPath '<RK_NESTING_ATTRIBUTE> to keyPath 'fieldName' -- value is unchanged (Youtube Link)
2013-10-11 09:54:45.564 MyMobile[45460:6207] T restkit.object_mapping:RKMappingOperation.m:497 Skipping attribute mapping for special keyPath '<RK_NESTING_ATTRIBUTE>'
2013-10-11 09:54:45.564 MyMobile[45460:6207] T restkit.object_mapping:RKMappingOperation.m:440 Mapping attribute value keyPath 'Youtube Link' to 'fieldValue'
2013-10-11 09:54:45.565 MyMobile[45460:6207] T restkit.object_mapping:RKMappingOperation.m:429 Found transformable value at keyPath 'Youtube Link'. Transforming from class '__NSCFString' to 'NSString'
2013-10-11 09:54:45.565 MyMobile[45460:6207] E restkit.object_mapping:RKMappingOperation.m:431 Failed transformation of value at keyPath 'Youtube Link' to representation of type 'NSString': (null)
2013-10-11 09:54:45.566 MyMobile[45460:6207] D restkit.object_mapping:RKMappingOperation.m:920 Finished mapping operation successfully...
4

1 回答 1

1

我最近遇到了类似的问题。使用动态属性映射时,有时值转换器似乎未正确初始化。

我通过在我的子类中将属性从更改NSString为类型找到了解决方法。例如,假设您的 NSManagedObject 通常看起来像这样:idNSManagedObject

@interface CustomValue : NSManagedObject

@property (nonatomic, retain) NSString * fieldValue;

@end

尝试将其更改为:

@interface CustomValue : NSManagedObject

@property (nonatomic, retain) id fieldValue;

@end

您还必须将您fieldValue的数据模型从内部更改StringTransformable.

在我这样做之后,映射仍然将类型转换为 anNSString但它是成功的。

于 2013-11-02T19:13:54.513 回答