简体   繁体   中英

RestKit 0.20 - Mapping with keyPath

This is my response body:

{
  "bsqn": 5,
  "results": [
    {
      "sqn": 1,
      "res": "oldu!"
    }
  ]
}

I wanna get through for "res" property which i really need, by using this keyPath:"bsqn.res"

//TestResponse.h
@interface TestResponse : NSObject

@property (nonatomic, copy) NSString *res;

+ (RKObjectMapping *)objectMapping;

@end

// TestResponse.m
@implementation TestResponse

+ (RKObjectMapping *)objectMapping
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];
    [mapping addAttributeMappingsFromDictionary:@{@"res": @"res"}];

    return mapping;
}

@end


RKResponseDescriptor *newDesc = [RKResponseDescriptor responseDescriptorWithMapping:    
[TestResponse objectMapping] method:RKRequestMethodPOST pathPattern:nil keyPath:@"bsqn.res"     
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

But it gives this error:

this class is not key value coding-compliant for the key hashcode

Is there a way to do what i want? Thanks..

In your JSON, bsqn.res doesn't exist. bsqn is just a string.

Change your response descriptor to keyPath:@"results" and RestKit will map each item in the results array into a new instance of your TestResponse and set the res property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM