为什么我的.plist没有显示已保存的数据?

时间:2014-04-21 21:42:45

标签: ios objective-c nsmutablearray plist writetofile

我有一个简单的待办事项列表,我在列表中添加了项目。我想在应用关闭时将这些项目保存到.plist文件中,以便在用户重新打开应用时显示这些项目。

我目前正在尝试两种不同的方法。两者都给我List successfully saved!个日志条目,但两个都没有在重新启动时显示文件。

方法1: 在我的mainAppDelegate.m

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *filePath = [basePath stringByAppendingPathComponent:@"toDoItems.plist"];

    NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    [plistdict writeToFile:filePath atomically:YES];


    if(![self.toDoItems writeToFile:filePath atomically:YES]) {
    NSLog(@"List Successfully Saved!");
    };
}

在我的XYZToDoListController.m

- (void)viewDidLoad
{
     [super viewDidLoad];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];
     NSFileManager *fileManager = [NSFileManager defaultManager];
     if ([fileManager fileExistsAtPath:filePath])

方法2: 在我的mainAppDelegate.m

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];


    if(![self.toDoItems writeToFile:filePath atomically:YES]) {
    NSLog(@"List Successfully Saved!");
    };
}

在我的XYZToDoListViewController

- (void)viewDidLoad
{
     [super viewDidLoad];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];
     NSFileManager *fileManager = [NSFileManager defaultManager];
     if ([fileManager fileExistsAtPath:filePath])
     {
        self.toDoItems = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
        NSLog(@"Loading saved list");
     } else {
        self.toDoItems = [[NSMutableArray alloc] init];
        NSLog(@"Empty List");
     }
}

我对使用目标C相当新,我已经阅读了很多问题和答案,但我似乎无法加载我的.plist文件。有人可以通过一个适用于我的情况的好例子来帮助我弄清楚我的代码中出了什么问题吗?

正如我所说,在日志输出中,我看到“空列表”方法正在加载,它告诉我“列表已成功保存!”。超出此范围的任何调试输出我都不确定如何使用。

由于

1 个答案:

答案 0 :(得分:1)

您正在将文件保存在NSDocumentsDirectory中(如果适用)但是当您加载它时,您只是从应用程序当前目录(可能是应用程序文件夹本身)加载它。您在阅读之前也写了一个空文件,尝试在您的应用代理中使用以下内容:

-(NSString*)toDoFilePath
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *filePath = [basePath stringByAppendingPathComponent:@"toDoItems.plist"];

    return filePath;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self toDoFilePath]])
    {
        self.toDoItems = [[NSMutableArray alloc]initWithContentsOfFile:[self toDoFilePath]];
        NSLog(@"Loading saved list");
    } else {
        self.toDoItems = [[NSMutableArray alloc] init];
        NSLog(@"Empty List");
    }

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    if([self.toDoItems writeToFile:[self toDoFilePath] atomically:YES]) {
        NSLog(@"List Successfully Saved!");
    };
}

假设(如图所示)您已在应用代理上拥有toDoItems属性。

要使用视图控制器中的数据,在包含AppDelegate.h文件后,您可以使用:

self.toDoItems = [(AppDelegate*) [[UIApplication sharedApplication] delegate] toDoItems];