6

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?
}
jspooner
  • 10,975
  • 11
  • 58
  • 81

2 Answers2

8

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]]; 
}
jspooner
  • 10,975
  • 11
  • 58
  • 81
  • 3
    For v0.9.3 the import should be `#import ` if you are using JSONKit alternately you can also just use `[response parsedBody]` – Ronnie Liew Aug 28 '11 at 18:25
  • 2
    and for the current version (0.10) you should use `#import ` with the `RKJSONParserJSONKit` object instead of the `RKJsonParser` – Manuel van Rijn May 25 '12 at 11:37
0

Support for array of strings was added on v0.10: Source

Or Arbel
  • 2,965
  • 2
  • 30
  • 40