2013-10-23 86 views
2

對RESTKit 0.2x有一些疑問。 RESTKit相對較新,所以請原諒我對某些主題的無知。RESTKit 0.2x陣列頂層映射

比方說,我有這樣的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,它沒有真正回答我所需要的。

===更新的誤差LOG ====

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.} 

==== UPDATE完====

回答

0

在你的JSON results是一個問題,因爲它是一個數組(所以你可以用一個keypath導航到它,它也不在你的keypath中,所以RestKit找不到正確的起點,除此之外你的映射沒有問題)

To處理results您需要更改JSON(從服務器或一旦收到)或爲結果創建映射(將地址映射應用於嵌套數據的關係)。

+0

嗯..我試着用'results.address_components'作爲keyPath,它仍然不起作用。你在談論做映射關係嗎? – Steven

+0

我更新了我的錯誤日誌。這是在我的'RKResponseDescriptor'中添加結果到keyPath後的結果 – Steven

+0

將keypath設置爲'results.address_components'將不起作用,因爲它會嘗試處理一個數組數組,並且這不是你所期望的。我在談論他們之間有兩種映射關係。 – Wain