我已成功将我的 plist 保存到文档目录中!现在我无法加载它

发布于 2024-10-05 01:46:42 字数 1115 浏览 7 评论 0原文

这段代码可以完美地将我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灵芸 2024-10-12 01:46:42
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TitleArray.plist"];
NSArray * myList = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [myList mutableCopy];
self.titles = tmpArray;
[tmpArray release];

没用?

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TitleArray.plist"];
NSArray * myList = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [myList mutableCopy];
self.titles = tmpArray;
[tmpArray release];

didn't work?

北座城市 2024-10-12 01:46:42

如果您询问如何覆盖应用程序主包中包含的 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?

铁轨上的流浪者 2024-10-12 01:46:42
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])  
{    
    docDirPath = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];
}  
NSMutableArray *tmpArray = [[[NSMutableArray    alloc] initWithContentsOfFile: docDirPath] mutableCopy];
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])  
{    
    docDirPath = [[NSBundle mainBundle] pathForResource:@"TitleArray" ofType:@"plist"];
}  
NSMutableArray *tmpArray = [[[NSMutableArray    alloc] initWithContentsOfFile: docDirPath] mutableCopy];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文