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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the targetObject for?