载入从plist文件数组

载入从plist文件数组

本文介绍了保存/载入从plist文件数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造我的文档directoy 2的plist文件,我想使用存储阵列其中。在第一次运行,而文件创建的arent还一切正常,项目将保存到阵列,然后被写入到文件的plist我可以在文件目录检查本之一。但是,当我从plist中读取,然后使用它我得到一个EXC_BAD_ACCESS错误。
我有以下的code:

在viewDidLoad中:

 的NSArray *路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    * NSString的documentsDirectory = [路径objectAtIndex:0];
      * NSString的arrayPath = [documentsDirectory stringByAppendingPathComponent:@Save1.plist];
     * NSString的arrayPath2 = [documentsDirectory stringByAppendingPathComponent:@Save2.plist];
  *的NSFileManager = fManager [的NSFileManager defaultManager]
如果([fManager fileExistsAtPath:arrayPath]&放大器;&放大器; [fManager fileExistsAtPath:arrayPath2]){
saveArray1 = [[NSMutableArray里的alloc] initWithContentsOfFile:arrayPath];
saveArray2 = [[NSMutableArray里的alloc] initWithContentsOfFile:arrayPath2];
}
其他{
saveArray1 = [[NSMutableArray里的alloc]初始化];
saveArray2 = [[NSMutableArray里的alloc]初始化];
}

后来我有

  [saveArray ADDOBJECT:东西];
[saveArray2 ADDOBJECT:something2];
 [saveArray1将writeToFile:arrayPath原子:YES];
 [saveArray2将writeToFile:arrayPath2原子:YES];


解决方案

这应该是 [saveArray1 ADDOBJECT:东西];
没有 [saveArray ADDOBJECT:东西];

I'm creating 2 plist files in my documents directoy which I'd like to use for storing arrays. On the first run, while the files arent created yet everything works fine, items get saved into the arrays and then get written to one of the plist files I can check this in the documents dir. But when I have to read from the plist and then use it I get an EXC_BAD_ACCESS error.I have the following code:

in 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];
}

And later I have

[saveArray addObject:something];
[saveArray2 addObject:something2];
 [saveArray1 writeToFile:arrayPath atomically:YES];
 [saveArray2 writeToFile:arrayPath2 atomically:YES];
解决方案

It should be [saveArray1 addObject:something];not [saveArray addObject:something];

这篇关于保存/载入从plist文件数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 17:32