这是我将 json 保存到 nscachesdirectory 中的 plist 的函数
-(void)saveproducts{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString* plistPath = [documentsPath stringByAppendingPathComponent:@"Products.plist"];
NSDictionary*dict = [NSDictionary dictionary];
NSMutableArray *types = [NSMutableArray array];
NSArray *dictArray = [NSArray array];
NSArray *dictKeys = [NSArray array];
NSArray *productObjects = [NSArray array];
NSArray *productKeys = [NSArray array];
for (int i=0; i<appDelegate.products.count; i++) {
NSMutableArray *tmpProds = [NSMutableArray array];
NSString *t_ID = [[appDelegate.products objectAtIndex:i] valueForKey:@"id"];
NSString *t_image = [[appDelegate.products objectAtIndex:i] valueForKey:@"image"];
NSString *t_name =[[appDelegate.products objectAtIndex:i] valueForKey:@"name"];
NSArray *products = [[appDelegate.products objectAtIndex:i] valueForKey:@"products"];
NSDictionary *productsDict = [NSDictionary dictionary];
for (int j=0; j<products.count; j++) {
NSString *p_id = [[products objectAtIndex:j] valueForKey:@"id"];
NSString *p_name = [[products objectAtIndex:j] valueForKey:@"name"];
NSString *p_name2 = [[products objectAtIndex:j] valueForKey:@"name2"];
NSString *image = [[products objectAtIndex:j] valueForKey:@"image"];
NSString *typeID = [[products objectAtIndex:j] valueForKey:@"type_id"];
NSString *active = [[products objectAtIndex:j] valueForKey:@"active"];
NSString *available = [[products objectAtIndex:j] valueForKey:@"available"];
NSString *desc = [[products objectAtIndex:j] valueForKey:@"description"];
NSString *price = [[products objectAtIndex:j] valueForKey:@"price"];
if ([p_name2 isEqual:[NSNull null]]) {
p_name2 =@"undefined";
}
if ([desc isEqual:[NSNull null]]) {
desc = @"";
}
productObjects = [NSArray arrayWithObjects:p_id,p_name,p_name2,image,typeID,active,available,desc,price, nil];
productKeys = [NSArray arrayWithObjects:@"id",@"name",@"name2",@"image",@"type_id",@"active",@"available",@"desc",@"price", nil];
productsDict = [NSDictionary dictionaryWithObjects:productObjects forKeys:productKeys];
[tmpProds addObject:productsDict];
if (![image isEqualToString:@""]) {
[foodImages addObject:image];
}
}
dictArray = [NSArray arrayWithObjects:t_ID,t_image,t_name,tmpProds, nil];
dictKeys = [NSArray arrayWithObjects:@"id",@"image",@"name",@"products", nil];
dict = [NSDictionary dictionaryWithObjects:dictArray forKeys:dictKeys];
[types addObject:dict];
}
NSDictionary* plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:types, nil] forKeys:[NSArray arrayWithObjects:@"ptype", nil]];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"cached"];
[defaults synchronize];
}
else
{
NSLog(@"Error log: %@", error);
}
}