About
The place where shares everything about mobile technology!
-(void) saveCacheWithJSON:(id)json {
if (!baseCacheName) {
DLog(@"HEY, baseCacheName is nil . DID YOU FORGET TO SET IT?");
return;
}
if([json isKindOfClass:[NSDictionary class]]) {
NSData *dataToSave = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
NSString *path = [PATH_DOCUMENT_FOLDER stringByAppendingFormat:@"/%@.json",baseCacheName];
[dataToSave writeToFile:path atomically:YES];
}
}
-(NSString*) loadCacheString {
if (!baseCacheName) {
DLog(@"HEY, baseCacheName is nil . DID YOU FORGET TO SET IT?");
return nil;
}
NSString *path = [PATH_DOCUMENT_FOLDER stringByAppendingFormat:@"/%@.json",baseCacheName];
return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
}
Note that saveCacheWithJSON function the parameter is id 'cause some people is using AFNetworking as a lib to connect BO so JSON string will be removed double quotes "". The best practise is use NSData.