Last active
January 28, 2018 23:38
-
-
Save kyleclegg/5846568 to your computer and use it in GitHub Desktop.
RestKit - Load data from local json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
What is the targetObject for?
thanks for the snippet!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.