无法将NSDictionary写入plist文件 [英] Not able to write NSDictionary to plist file

查看:237
本文介绍了无法将NSDictionary写入plist文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:当我更改以下行时,我能够将其写入文件

Update: I am able to write tot he file when I change the following line

[infoDict setObject:userList forKey:@"users"];

[infoDict setObject:@"sampleString" forKey:@"users"];

所以,我要写入文件的结构一定有问题,有什么想法吗?

so, there must be problem with the structure I'm trying to write to the file, any thoughts?

我正在尝试创建一个plist文件,并在运行时在我的iOS应用中将其写入.我不能写它,但是我可以阅读. writeToFile方法返回"NO". 当我手动尝试手动打开使用Finder创建的文件时,它说无法读取数据,因为格式不正确."据我了解,因为使用文本编辑器打开文件时,为空,不显示plist类型的文件应具有的任何xml标记. 而且我知道我可以从中读取内容,因为在手动将有效" plist文件拖动到文件夹中后,我的代码就可以从其中读取一个空列表.

I am trying to create a plist file and write to it at runtime in my iOS app. I am not able to write to it but I'm able to read though. The writeToFile method returns a 'NO'. When I manually try to manually open the file I created using Finder, it says "The data couldn’t be read because it isn’t in the correct format.", which I understand because on opening the file using a text editor it is empty and doesn't show any xml tags which a file of type plist should have. And I know I can read from it because on manually dragging a 'valid' plist file in the folder, my code is able to read an empty list from there.

1)为什么不创建有效的plist文件?

1) Why isn't it creating a valid plist file?

2)为什么我无法写入(甚至是有效的)plist文件?

2) Why am I not able to write to (even a valid) plist file?

    NSLog(@"* Writing contents to plist file *");

    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* plistPath = nil;

    if ((plistPath = [path stringByAppendingPathComponent:@"users.plist"]))
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];

        BOOL exists = [fileManager fileExistsAtPath:plistPath];
        NSLog(exists ? @"yes, file exists": @"no, file doesnt exist");

        if (!exists) {
            NSLog(@"creating file since it doesnt exist");
            [fileManager createFileAtPath:plistPath contents:nil attributes:nil];
        }

        NSLog(@"path of file: %@", plistPath);
        NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] init];
        NSLog(@"infoDict: %@", infoDict);

        [infoDict setObject:userList forKey:@"users"];
        NSLog(@"infoDict: %@", infoDict);

        BOOL wrote = [infoDict writeToFile:plistPath atomically:YES];
        NSLog(wrote ? @"yes, wrote to the file": @"no, didn't write to file");
   }
    NSLog(@"--------------------------------------------");

以下是控制台中的输出:

Following is the output in the console:

    2014-10-17 10:50:11.290 UserReggy[1228:10032] * Writing contents to plist file *
2014-10-17 10:50:11.291 UserReggy[1228:10032] yes, file exists
2014-10-17 10:50:11.291 UserReggy[1228:10032] path of file: /Users/rvyas1/Library/Developer/CoreSimulator/Devices/DB612284-F481-467A-BAE8-37750497F42A/data/Containers/Data/Application/C9620C4D-EF1F-4C17-8D28-4E9B3B41F2C2/Documents/users.plist
2014-10-17 10:50:11.291 UserReggy[1228:10032] infoDict: {
}
2014-10-17 10:50:11.291 UserReggy[1228:10032] infoDict: {
    users =     {
        dfgdfgd = "<User: 0x7ffd884da820>";
    };
}
2014-10-17 10:50:11.292 UserReggy[1228:10032] no, didn't write to file
2014-10-17 10:50:11.294 UserReggy[1228:10032] --------------------------------------------

推荐答案

问题出在NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

它将为您提供空对象,因为您的plistPath为零

it will give you null object beacause your plistPath is nil

这篇关于无法将NSDictionary写入plist文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆