0

我正在尝试将 NSMutable Array 和 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 进行初始化。这是我从字典中提取值的方式吗?

4

2 回答 2

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);
}

}

于 2013-10-03T01:32:51.803 回答
0

还有这个阅读清单

-(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];

}

于 2013-10-03T01:37:52.437 回答