Search code examples
iosobjective-crestkit-0.20kvc

How do I fetch a simple json array of strings using restkit


My question is a duplicate of

RestKit: how to fetch a simple JSON array of strings?

However, while the original author found the answers helpful, I'm still lost. This question features two separate references, neither of which went far enough to help me (a complete beginner at KVC and RestKit).

One reference is here and gives this example code

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Whatever class]];
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"someAttribute"]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"/whatever" keyPath:nil statusCodes:nil];

What is Whatever class supposed to be? Should it be NSString? Or is this implying I should create a wrapper class that has one property which is an NSArray *?

And what is @"someAttribute" supposed to be? Again, is this implying a wrapper class in which case someAttribute would be the name of the property on my wrapper class that is an NSArray *?

The other reference is to here and while it shows fetching from an array into a "wrapper class", the wrapper class only has one NSNumber, not an array. So it's not clear how to turn that example into one that works for getting the whole array.

For reference the server is returning an array of strings just like the original question: ["string1", "string2", "string3"].

UPDATE

Here is code I've attempted. I've created a wrapper class.

@interface CannedResponse : NSObject

@property (strong, nonatomic) NSString * response;

@end

Here is the mapping:

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[CannedResponse class]];
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"response"]];
[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:RESPONSES_API_PATH keyPath:nil statusCodes:nil;

And I'm calling getObjectsAtPathForRouteName

What I'm getting back is an array of CannedReponses as expected. However rather than an NSString being put into the response property. I'm getting an RKMappingSourceObject. Now the RKMappingSourceObject does indeed contain the correct string.

I'll be looking through the RestKit logging in the console to see if I can glean some more clues.

UPDATE#2

As mentioned here by blakewatters I'm not doing any mapping in this example and can just bypass the use of RestKit. The problem is that all the code I'm writing in the service layer is using RestKit and I was blindly following the pattern for all calls. For this call the right answer might be to figure out how to access AFNetworking directly.


Solution

  • Here is a better answer using another comment from blakewatters here

    Don't use RestKit for a request like this. Use AFNetworking to do it. And you can access the AFHTTPClient like this

    [RKObjectManager sharedManager].HTTPClient
    

    The code then just looks something like this

    AFHTTPClient * client = [RKObjectManager sharedManager].HTTPClient;
    [client getPath:YOUR_PATH
         parameters:nil
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                success([NSArray arrayWithArray:responseObject]);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                failure(operation.response.statusCode);
            }];