对 RESTKit 0.2x 有一些疑问。RESTKit 相对较新,所以请原谅我对某些主题的无知。
假设我有这个 JSON(这个例子来自谷歌地理编码,url 是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.}
==== 更新结束 ====