从 plist 文件保存/加载数组
我正在我的文档目录中创建 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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该是
[saveArray1 addObject:something];
不是
[saveArray addObject:something];
It should be
[saveArray1 addObject:something];
not
[saveArray addObject:something];