Saving/loading an array from plist file

二次信任 提交于 2019-12-11 09:49:56

问题


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:


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



来源:https://stackoverflow.com/questions/6595701/saving-loading-an-array-from-plist-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!