【问题标题】:Why isn't my .plist showing the saved data?为什么我的 .plist 没有显示保存的数据?
【发布时间】:2014-04-21 21:42:44
【问题描述】:

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

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

方法一: 在我的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])

方法二: 在我的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");
     }
}

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

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

谢谢

【问题讨论】:

    标签: ios objective-c nsmutablearray plist writetofile


    【解决方案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];
    

    【讨论】:

    • 更好的是,只需调用一个应用程序委托方法即可将路径返回到 plist 文件并将所有加载/保存移动到应用程序委托中。
    • 您能否举例说明您在评论中的意思?您的意思是通过 mainAppDelegate 加载数据?我尝试使用我发布的两种方法来添加您编写的 NSDocumentDirectory 路径,但我仍然没有加载文件。
    • 再看一遍你的代码,你也在读(不存在的)文件,就在你写之前。
    • 通过您的编辑,我没有看到任何“列表已成功保存!”登录我的输出。
    • 因为当我复制你的代码时,我复制了你错误的成功检查,writeToFile 在成功时返回 true,而不是 false。我已经修复了这个例子。也可能最好不要在 applicationWillTerminate 中保存,而是在数据更新时保存,但我已将其保留为读者练习:)
    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多