当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - RestKit - Entity Mapping Property transformation

问题描述:

I need to map a web service with object properties all in caps against coredata object properties all in lowercase:

# JSON

{

'ID': 'value',

'TITLE': 'value',

'BODY': 'value',

}

# CoreData Entity

{

'id': 'value',

'title': 'value',

'body': 'value',

}

I mapped the fields by doing:

RKEntityMapping *entMap = [RKEntityMapping mappingForEntityForName:entName

inManagedObjectStore:managedObjectStore];

NSEntityDescription *entity = [[managedObjectModel entitiesByName]

objectForKey:entName];

[entMap addAttributeMappingsFromArray:[[[RKPropertyInspector sharedInspector]

propertyInspectionForEntity:entity] allKeys]];

I saw this convenient setDefaultSourceToDestinationKeyTransformationBlock fonction in RKObjectMapping allowing to define custom transformation on object properties. This is not available on RKEntityMapping.

How can I do property transformations with RKEntityMapping without defining fields manually ?

网友答案:

Here is my current work-around:

- (NSDictionary *)dictToArray:(NSArray *)array
    withTranformation:(NSString *(^)(NSString *))theBlock
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    for (NSString *key in array) {
        dict[theBlock(key)] = key;
    }
    return dict;
}

NSArray *fields = [[[RKPropertyInspector sharedInspector] 
    propertyInspectionForEntity:entity] allKeys];
NSDictionary *mapDict = [self dictToArray:fields
    withTranformation:^NSString *(NSString *str) {
        return [str uppercaseString];
    }];

[entMap addAttributeMappingsFromDictionary:mapDict];
分享给朋友:
您可能感兴趣的文章:
随机阅读: