我试图将NSMutable数组和NSString保存到plist中,然后如果它存在则从plist初始化值。但是,当我重新运行应用程序时,值不会按照预期进行初始化。到目前为止,以下是我所拥有的。
if (self = [super init]) {
NSString *path=[self getFileUrl];
NSFileManager *fm = [[NSFileManager alloc] init];
if ([fm fileExistsAtPath:path]) {
_history = [d objectForKey:@"stringKey"];
class=[d objectForKey: @"ArrayKey"];
}
NSDictionary *d;
但是,根据plist,值不会被初始化。这是我从字典中提取值的方式吗?
答案 0 :(得分:0)
这是我的函数,它将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);
}
}
答案 1 :(得分:0)
和这一个阅读plist
-(void)loadFromplist{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString* plistPath = [documentsPath stringByAppendingPathComponent:@"Products.plist"];
NSData *plistData = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *dict = (NSDictionary*)[NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:nil errorDescription:nil];
productsArray = [dict valueForKey:@"ptype"];
[self.tableView reloadData];
}