开发者

Saving/loading an array from plist file

开发者 https://www.devze.com 2023-03-17 11:23 出处:网络
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 arra

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];
[sav开发者_如何学GoeArray2 addObject:something2];
 [saveArray1 writeToFile:arrayPath atomically:YES];
 [saveArray2 writeToFile:arrayPath2 atomically:YES];


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号