2013-10-11 52 views
0

我正在使用RestKit v0.21,並嘗試使用動態名稱映射值數組。我能夠正確地獲得自定義字段的名稱,但我無法捕獲關聯的值。該JSON看起來是這樣的:Restkit動態屬性映射

{ 
    "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]]; 

所有我見過的,具有動態屬性的例子包括值的對象,其中值映射會是什麼樣「(文件名).email」。在我的情況下,它總是隻是一個字符串:字符串名稱和值的集合,表示一組完全動態的自定義字段和值。

當我檢查自定義字段對象的集合fieldName屬性設置,但fieldValue屬性都是(空)。

任何想法?

更新:這是在映射自定義字段的數組中的元素之一日誌輸出:

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... 
+0

不知道它是否會工作,但你可以嘗試'attributeMappingFromKeyPath:nil toKeyPath:@「fieldValue」'。需要逐步完成映射過程,看看哪些數據可用... – Wain

+0

我試過: '[customFieldMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:無toKeyPath:@ 「fieldValue方法」];' 但事與願違許多任何差異。映射過程的源代碼中的位置將是一個很好的地方,以便在逐步完成時查看? – scubasteve

+0

打開映射的跟蹤記錄並查看通過RKMappingOperation.m進行調試 – Wain

回答

1

我最近遇到了類似的問題。似乎有時使用動態屬性映射時,值轉換器沒有正確初始化。

我在NSManagedObject的子類中找到了一個解決方法,將屬性NSString更改爲id。例如,假設您的NSManagedObject通常是這樣的:

@interface CustomValue : NSManagedObject 

@property (nonatomic, retain) NSString * fieldValue; 

@end 

試試這個更改爲:

@interface CustomValue : NSManagedObject 

@property (nonatomic, retain) id fieldValue; 

@end 

你也有你的fieldValue你的數據模型內改變從StringTransformable

我做了這個之後,映射仍然將該類型轉換爲NSString,但它成功了。

+0

這將是一個很好的例子,你做了什麼。 –

+0

完成建議:) – Cheblon