Skip to content

Instantly share code, notes, and snippets.

@kyleclegg
Last active January 28, 2018 23:38
Show Gist options
  • Select an option

  • Save kyleclegg/5846568 to your computer and use it in GitHub Desktop.

Select an option

Save kyleclegg/5846568 to your computer and use it in GitHub Desktop.
RestKit - Load data from local json file
NSString *filePath = [[NSBundle mainBundle] pathForResource:JohnSmith ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
if (data) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
KCUser *appUser = [[KCUser alloc] init];
NSString* MIMEType = @"application/json";
NSError* error;
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error];
if (parsedData == nil && error) {
NSLog(@"parser error");
}
NSDictionary *mappingsDictionary = @{ @"appUser": [KCUser mapping] };
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
mapper.targetObject = appUser;
NSError *mappingError = nil;
BOOL isMapped = [mapper execute:&mappingError];
if (isMapped && !mappingError) {
[[KCUserSingleton sharedKCUserSingleton] setUser:appUser];
}
else {
NSLog(@"error desc: %@", error.localizedDescription);
NSLog(@"error reason: %@", error.localizedFailureReason);
NSLog(@"error suggestion: %@", error.localizedRecoverySuggestion);
}
}
@kyleclegg
Copy link
Copy Markdown
Author

@woodmister1 - I like to setup my RestKit mappings within each model class rather than all at the time of configuration, which could get massive for a larger project. Then, I create a public class method to return the mapping for that class. Does that make sense? That is what I am calling on line 15. I can show my model class as well if you'd like.

@tolentinokas
Copy link
Copy Markdown

What is the targetObject for?

@slxl
Copy link
Copy Markdown

slxl commented Jan 28, 2018

thanks for the snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment