Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit - Load a simple array

I'm using RestKit into my iPhone application to load a list of countries. The problem is the elementToPropertyMappings method uses a dictionary to map each object. In my case I have an array of strings that I'd like to map to the name property on my Country class.

Anyone know how todo this?

elementToPropertyMappings

Must return a dictionary containing mapping from JSON element names to property accessors

  • (NSDictionary *)elementToPropertyMappings Declared In RKObjectMappable.h

My JSON Data

["Argentina","Australia","Austria","Belgium","Bolivia","Brazil","Bulgaria","Canada","Cayman Islands","China","Costa Rica","Croatia","Czech Republic","Denmark","Ecuador","Ethiopia","F.Y.R.O. Macedonia","Finland","France","French Polynesia","Germany","Guam","Hong Kong SAR","Indonesia","Ireland","Israel","Italy","Japan","Latvia","Lithuania","Luxembourg","Malaysia","Malta","Mexico","Morocco","Netherlands","New Zealand","Nicaragua","Norway","Papua New Guinea","Peru","Poland","Portugal","Puerto Rico","Qatar","Romania","Russia","Singapore","Slovakia","Slovenia","South Africa","South Korea","Spain","Sweden","Switzerland","Taiwan","United Arab Emirates","United Kingdom","United States","Venezuela","Vietnam"]

UPDATE:

I figured out how to use the RKClient to make a request so the Mapping functionality is skipped. Now I need to figure out what class to use for JSON parsing. The yajl-objc parser looks great but I don't want to include another parser if it can be done with a lib from RestKit.

-(void)loadLocations
{
    NSLog(@"loadLocations");
    RKObjectManager *objectManager = [RKObjectManager sharedManager];    
    [[RKClient sharedClient] get:@"/locations/countries.json" delegate:self];

}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
    NSLog(@"Loaded payload: %@", [response bodyAsString]);
//    HOW CAN I PARSE THIS STRING INTO AN NSArray?
}
like image 774
jspooner Avatar asked May 20 '11 05:05

jspooner


1 Answers

Figuring out the proper import for RKJSONParser was the most challenging thing for me.

If there is another way to accomplish this with the Mapping classes please let me know.

Here is the code involved with loading a simple array.

#import <RestKit/Support/RKJSONParser.h> 
@implementation CountriesViewController
@synthesize countries;

-(void)loadLocations
{
    NSLog(@"loadLocations");    
    [[RKClient sharedClient] get:@"/locations/countries.json" delegate:self];
}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
    NSLog(@"Loaded payload: %@", [response bodyAsString]);
    RKJSONParser* parser = [RKJSONParser new]; 
    countries =    [parser objectFromString:[response bodyAsString]]; 
}
like image 101
jspooner Avatar answered Sep 21 '22 02:09

jspooner