0

我需要将对象属性全部大写的 Web 服务映射到 coredata 对象属性全部小写:

# JSON
{
    'ID': 'value',
    'TITLE': 'value',
    'BODY': 'value',
}
# CoreData Entity
{
    'id': 'value',
    'title': 'value',
    'body': 'value',
}

我通过以下方式映射了字段:

RKEntityMapping *entMap = [RKEntityMapping mappingForEntityForName:entName 
    inManagedObjectStore:managedObjectStore];
NSEntityDescription *entity = [[managedObjectModel entitiesByName] 
    objectForKey:entName];
[entMap addAttributeMappingsFromArray:[[[RKPropertyInspector sharedInspector]
    propertyInspectionForEntity:entity] allKeys]];

setDefaultSourceToDestinationKeyTransformationBlock我在RKObjectMapping允许在对象属性上定义自定义转换时看到了这个方便的功能。这在 上不可用RKEntityMapping

如何在RKEntityMapping不手动定义字段的情况下进行属性转换?

4

1 回答 1

0

这是我目前的解决方法:

- (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];
于 2013-05-27T10:36:00.217 回答