RESTKit 0.2x数组的顶级映射

时间:2013-10-23 15:15:44

标签: ios restkit restkit-0.20

对RESTKit 0.2x有一些疑问。 RESTKit相对较新,请原谅我对某些主题的无知。

假设我有这个JSON(此示例来自Google地理编码,网址为https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true):

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "1600",
                    "short_name": "1600",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Amphitheatre Parkway",
                    "short_name": "Amphitheatre Pkwy",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Mountain View",
                    "short_name": "Mountain View",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Santa Clara County",
                    "short_name": "Santa Clara County",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "California",
                    "short_name": "CA",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "United States",
                    "short_name": "US",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "94043",
                    "short_name": "94043",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
            "geometry": {
                "location": {
                    "lat": 37.4219988,
                    "lng": -122.083954
                },
                "location_type": "ROOFTOP",
                "viewport": {
                    "northeast": {
                        "lat": 37.42334778029149,
                        "lng": -122.0826050197085
                    },
                    "southwest": {
                        "lat": 37.42064981970849,
                        "lng": -122.0853029802915
                    }
                }
            },
            "types": [
                "street_address"
            ]
        }
    ],
    "status": "OK"
}

我正在寻找的是将“long_name”数组映射到CoreData,其中NSManagedObject的字符串名为“long_name”。

原因是我的后端API标准将包含一些元数据以及信息,并且我想在第一级(或者在这种情况下,两层深)之后映射到CoreData。简而言之,对于“你为什么不创建一个”address_components“对象然后将数组映射到它的答案,这对我来说无效。

所以使用这个JSON,我将在CD中有三行存储不同的长名称。为了简短起见,我限制了主要ID。 我最终得到这样的错误:

"Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 \"The given value is not already an instance of 'NSString'\" UserInfo=0x9b808c0 {NSLocalizedDescription=The given value is not already an instance of 'NSString'}",

这是我的映射数据的样子:

+ (RKObjectMapping *)mapping {
    // Create a singleton instance of the mapping object.
    __strong static RKEntityMapping *_mapping = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        RKManagedObjectStore *store = [[RKObjectManager sharedManager] managedObjectStore];
        _mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:store];
        [_mapping addAttributeMappingsFromDictionary:@{@"long_name": @"long_name"}];

    });
    return _mapping;
}

我到达的响应描述符:

[objectManager addResponseDescriptorsFromArray:@[[RKResponseDescriptor responseDescriptorWithMapping:[object mapping] // from the mapping function up top
    method:RKRequestMethodGET
    pathPattern:kIRISAPIResourcePath_GoogleElevationAPI
    keyPath:@"results.address_components"
    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]];

我对此非常感激不已。思考?我看一下https://github.com/RestKit/RestKit/wiki/Object-Mapping#mapping-values-without-key-paths并没有真正回答我的需要。

===更新错误日志====

2013-10-23 15:29:08.226 IRIS[1518:f03] D restkit.object_mapping:RKPropertyInspector.m:131 Cached property inspection for Class 'GoogleMaps': {
    long_name = {
        isPrimitive = 0;
        keyValueCodingClass = NSString;
        name = long_name;
    };
}
2013-10-23 15:29:08.226 IRIS[1518:3a0f] E restkit.object_mapping:RKMappingOperation.m:431 Failed transformation of value at keyPath 'long_name' to representation of type 'NSString': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value '(
    1600,
    "Amphitheatre Parkway",
    "Mountain View",
    "Santa Clara County",
    California,
    "United States",
    94043
)' to NSString: none of the 2 value transformers consulted were successful." UserInfo=0x991d270 {detailedErrors=(
    "Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 \"The given value is not already an instance of 'NSString'\" UserInfo=0x9915ff0 {NSLocalizedDescription=The given value is not already an instance of 'NSString'}",
    "Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3000 \"Expected an `inputValue` of type `NSNull`, but got a `__NSArrayI`.\" UserInfo=0x9932900 {NSLocalizedDescription=Expected an `inputValue` of type `NSNull`, but got a `__NSArrayI`.}"
), NSLocalizedDescription=Failed transformation of value '(
    1600,
    "Amphitheatre Parkway",
    "Mountain View",
    "Santa Clara County",
    California,
    "United States",
    94043
)' to NSString: none of the 2 value transformers consulted were successful.}

====更新结束====

1 个答案:

答案 0 :(得分:0)

您的JSON中的

results是一个问题,因为它是一个数组(因此您可以使用keypath导航到它。当前它也不在您的keypath中,因此RestKit找不到正确的起点。比你的映射还好。

要处理results,您需要更改JSON(从服务器或一旦收到)或创建结果的映射(使用关系将地址映射应用于嵌套数据)。