2013-10-23 2 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" 
} 

그리고 내가 무엇을 찾고 있어요 것은이 CoreData에 "긴 이름"의 배열을 매핑하는 것이다 NSManagedObject에 "long_name"이라는 문자열이 있습니다.

이유는 내 백엔드 API 표준에 정보와 함께 일부 메타 데이터가 포함되어 있으며 CoreData에 첫 번째 수준 (또는이 경우 두 단계 아래)을 매핑하려고하기 때문입니다. 즉, "address_components"개체를 만든 다음 배열을 매핑하면 왜 저를 위해 작동하지 않습니다.

따라서이 JSON에서는 CD로 세 행을 갖게됩니다. .. 다른 긴 이름을 저장 짧게 유지하기 위해, 나는 을 기본 ID를 제한하고 그리고이 같은 오류 치울 :

+ (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; 
} 
: 내 매핑 데이터 모양을 어떻게

"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'}", 

이것은이다

그리고 여기에있는 응답 설명자 :

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

==== 업데이트는 배열이기 때문에 당신의 JSON에서

답변

0

results이 문제가 ====에게 ENDS (keypath로 탐색 할 수 있습니다.) 또한 현재 Keypath에 있지 않으므로 RestKit에서 올바른 시작점을 찾지 못합니다. 매핑 이외에 괜찮 으면

To results을 처리하려면 JSON을 변경하거나 (서버에서 수신했거나 한 번받은 경우) 결과에 대한 매핑을 작성해야합니다 (중첩 된 데이터에 주소 매핑을 적용하는 관계로).

+0

hmm..i,'results.address_components'를 keyPath로 시도했지만 여전히 작동하지 않습니다. 지도 작성 관계를 말하는거야? – Steven

+0

오류 로그를 업데이트했습니다. 이것은 내가'RKResponseDescriptor'의 keyPath에 결과를 추가 한 후입니다. – Steven

+0

keypath를'results.address_components'로 설정하면 배열의 배열을 처리하려고하는데 그것이 당신이 기대하는 매핑이 아니기 때문에 작동하지 않을 것입니다. 나는 그들 사이의 관계로 2 개의 매핑을 갖는 것에 대해 이야기하고있다. – Wain