Skip to content

Instantly share code, notes, and snippets.

@kyleclegg
Last active January 28, 2018 23:38
Show Gist options
  • Save kyleclegg/5846568 to your computer and use it in GitHub Desktop.
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);
}
}
@tolentinokas
Copy link

What is the targetObject for?

@slxl
Copy link

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