Saving/loading an array from plist file

2019-08-13 17:02发布

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

1条回答
男人必须洒脱
2楼-- · 2019-08-13 17:32

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

查看更多
登录 后发表回答