Mapping a JSON response to an object using RestKit and Objective-C

前端 未结 2 889
庸人自扰
庸人自扰 2020-12-30 18:17

I am relatively new to Objective-C and am attempting to use RestKit to receive a JSON response from a web service. I have successfully received the data back to my applicat

2条回答
  •  旧巷少年郎
    2020-12-30 18:30

    The snippet of your code seems a bit outdated. I strongly recommend reading the newest Object Mapping guide in order to leverage RestKit into it's fullest potential - especially the part Mapping without KVC.

    Edit:

    In order to post an object with RestKit and receive back an answer, we define a TranslationRequest class that will hold our request & Translation to hold our response.

    Firstly, we set up our RKObjectManager and mappings (i usually do this in my AppDelegate):

    RKObjectManager *manager = [RKObjectManager objectManagerWithBaseURL:kOurBaseUrl];
    [manager setSerializationMIMEType:RKMIMETypeJSON];
    //this is a singleton, but we keep the manager variable to avoid using [RKObjectManager sharedManager] all the time
    
    //Here we define a mapping for the request. Note: We define it as a mapping from JSON to entity and use inverseMapping selector later.
    RKObjectMapping *translationRequestMapping = [RKObjectMapping mappingForClass:[TranslationRequest class]];
    [translationRequestMapping mapKeyPath:@"RegionTag" toAttribute:@"regionTag"];
    ...
    [[manager mappingProvider] setSerializationMapping:[translationRequestMapping inverseMapping] forClass:[TranslationRequest class]];
    
    //now we define the mapping for our response object
    RKObjectMapping *translationMapping = [RKObjectMapping mappingForClass:[Translation class]];
    [translationMapping mapKeyPath:@"id" toAttribute:@"identifier"];
    [translationMapping mapKeyPath:@"Translation" toAttribute:@"translation"];
    [[manager mappingProvider] addObjectMapping:mapping];
    
    //finally, we route our TranslationRequest class to a given endpoint
    [[manager router] routeClass:[TranslationRequest class] toResourcePath:kMyPostEndpoint];
    

    This should be enough of the necessary setup. We can call our backend anywhere in the code (e.g. in any controller) like this:

    //we create new TranslationRequest
    TranslationRequest *request = [[TranslationRequest alloc] init];
    [request setRegionTag:@"Hello"];
    ....
    //then we fetch the desired mapping to map our response with
    RKObjectMapping *responseMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:class]
    //and just call it. Be sure to let 'self' implement the required RKObjectManagerDelegate protocol
    [[RKObjectManager sharedManager] postObject:request mapResponseWith:responseMapping delegate:self];]
    

    Try this approach and let me know if you need any assistance.. I was not able to test it fully as i don't have any suitable backend that will return the responses, but judging from the RestKit log this should work.

提交回复
热议问题