从plist文件保存/加载数组

时间:2011-07-06 11:34:27

标签: arrays plist loading

我正在我的文档directoy中创建2个plist文件,我想用它来存储数组。在第一次运行时,虽然文件没有创建但一切正常,项目保存到数组中,然后写入其中一个plist文件,我可以在文档目录中检查。但是,当我必须从plist读取然后使用它时,我得到一个EXC_BAD_ACCESS错误。 我有以下代码:

在viewDidLoad中:

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
      NSString *arrayPath = [documentsDirectory stringByAppendingPathComponent:@"Save1.plist"];
     NSString *arrayPath2 = [documentsDirectory stringByAppendingPathComponent:@"Save2.plist"];
  NSFileManager *fManager = [NSFileManager defaultManager];
if([fManager fileExistsAtPath:arrayPath] && [fManager fileExistsAtPath:arrayPath2]) {
saveArray1 = [[NSMutableArray alloc] initWithContentsOfFile:arrayPath];
saveArray2 = [[NSMutableArray alloc] initWithContentsOfFile:arrayPath2];
}
else {
saveArray1 = [[NSMutableArray alloc] init];
saveArray2 = [[NSMutableArray alloc] init];
}

后来我有了

[saveArray addObject:something];
[saveArray2 addObject:something2];
 [saveArray1 writeToFile:arrayPath atomically:YES];
 [saveArray2 writeToFile:arrayPath2 atomically:YES];

1 个答案:

答案 0 :(得分:1)

它应该是[saveArray1 addObject:something]; 不是[saveArray addObject:something];