我已成功将我的 plist 保存到文档目录中!现在我无法加载它
这段代码可以完美地将我的 plist 保存到文档目录中。
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
NSString *path = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.titles = tmpArray;
[tmpArray release];
//TESTING NEW CODE FOR SAVING TO DOC DIR
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:@"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(@"plist is copied to Directory");
}
我一直无法弄清楚如何将 plist 加载回应用程序!
有人可以帮忙吗?谢谢。
This code works beautifully to save my plist to the documents directory.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
NSString *path = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.titles = tmpArray;
[tmpArray release];
//TESTING NEW CODE FOR SAVING TO DOC DIR
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:@"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(@"plist is copied to Directory");
}
I have not been able to figure out how to load the plist back to the app!
Can anyone help? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没用?
didn't work?
如果您询问如何覆盖应用程序主包中包含的 plist,那么您应该知道这是无法完成的。查看此问题以了解更多详细信息:您可以更新文件在应用程序包中?
If you are asking how to overwrite a plist included in your app's main bundle, then you should know that it cannot be done. Take a look at this question for more details: Can you update a file in the application bundle?