【问题标题】:RESTKit 0.2x top level mapping of arraysRESTKit 0.2x 顶级数组映射
【发布时间】:2013-10-23 15:15:44
【问题描述】:

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

假设我有这个 JSON(这个例子来自谷歌地理编码,网址是 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.}

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

【问题讨论】:

    标签: ios restkit restkit-0.20


    【解决方案1】:

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

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

    【讨论】:

    • hmm..我试过了,用results.address_components 作为keyPath,它仍然不起作用。你是说做映射关系吗?
    • 我已经更新了我的错误日志。这是在我将结果添加到我的 RKResponseDescriptor 中的 keyPath 之后
    • 将密钥路径设置为results.address_components 将不起作用,因为它将尝试处理数组数组,而这不是您映射所期望的。我说的是有 2 个映射以及它们之间的关系。
    • 我会将其标记为答案,因为address_componentresults 之间创建的关系修复了映射失败错误。但是,@Wain,你知道我是否可以直接映射到results 部分中显示的数组而不在 RESTKit 中建立关系?就像它是通过绕过results 而不使用KVC 进行映射,然后将其值视为第一级数组。希望我在那里说的话是有道理的。
    • 如果没有关系,您将只有 KVC。所以你基本上只有一个没有映射的标准 JSON 解析。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2017-12-15
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    相关资源
    最近更新 更多