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

时间:2010-11-27 03:43:41

标签: objective-c cocoa plist saving-data

此代码可以很好地将我的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加载回应用程序!

有人可以帮忙吗?感谢。

2 个答案:

答案 0 :(得分:4)

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

不起作用?

答案 1 :(得分:0)

如果您询问如何覆盖应用程序主包中包含的plist,那么您应该知道无法完成。请查看此问题以获取更多详细信息:Can you update a file in the application bundle?