Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're correct in thinking that two mappings is the answer. Assuming you are using the newest versions of RestKit, something like the following will work.</p> <p>Also keep in mind that in order to really leverage what RestKit has to offer, the JSON must be Key Value Coding compliant. For instance, in this example the JSON is wrapped in a "Response" key.</p> <p>I just pulled this from a project I'm working on and changed all of the names. This should get you going on the right path.</p> <pre><code>RKObjectMapping *postObjectMapping = [RKObjectMapping requestMapping]; [postObjectMapping addAttributeMappingsFromDictionary:@{ @"param1" : @"param1", @"param2" : @"param2"}]; RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:postObjectMapping objectClass:[PostObjectModel class] rootKeyPath:nil]; RKObjectMapping *returnedObjectMapping = [RKObjectMapping mappingForClass:[ReturnedModel class]]; [returnedObjectMapping addAttributeMappingsFromDictionary:@{ @"returnedParam1" : @"returnedParam1", @"returnedParam2": @"returnedParam2", @"returnedParam3": @"returnedParam3"}]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:returnedObjectMapping pathPattern:nil keyPath:@"Response" statusCodes:[NSIndexSet indexSetWithIndex:200]]; RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]]; [errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error" toKeyPath:@"errorMessage"]]; NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError); RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:@"errors" statusCodes:statusCodes]; PostModel *objectToBePosted = [PostModel new]; [login setParam1:something]; [login setParam2:something]; RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://BASEURLHERE"]]; [objectManager addRequestDescriptor:requestDescriptor]; [objectManager addResponseDescriptor:responseDescriptor]; [objectManager addResponseDescriptor:errorDescriptor]; NSMutableURLRequest *request = [objectManager requestWithObject:objectToBePosted method:RKRequestMethodPOST path:@"/REST/OF/PATH/GOES/HERE" parameters:nil]; RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSLog(@"Success block: %@", mappingResult); } failure: ^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"Failed with error: %@", [error localizedDescription]); }]; [objectManager enqueueObjectRequestOperation:operation]; </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload