【问题标题】:Not able to write NSDictionary to plist file无法将 NSDictionary 写入 plist 文件
【发布时间】:2014-10-27 18:53:30
【问题描述】:

更新:当我更改以下行时,我可以写入文件

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

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

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


我正在尝试在我的 iOS 应用程序中创建一个 plist 文件并在运行时写入它。我无法写信,但我可以阅读。 writeToFile 方法返回“否”。 当我手动尝试手动打开使用 Finder 创建的文件时,它显示“无法读取数据,因为它的格式不正确。”,我理解这是因为使用文本编辑器打开文件时为空并且不显示 plist 类型的文件应具有的任何 xml 标记。 而且我知道我可以从中读取,因为在文件夹中手动拖动“有效”plist 文件时,我的代码能够从那里读取一个空列表。

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

和,

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

    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(@"--------------------------------------------");

以下是控制台中的输出:

    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] --------------------------------------------

【问题讨论】:

    标签: ios objective-c xcode nsdictionary plist


    【解决方案1】:

    您可以在 userList 中检查您的数据,它必须是属性列表对象(NSData、NSDate、NSNumber、NSString、NSArray 或 NSDictionary 的实例),文档在the NSMutableDictionary Reference

    【讨论】:

    • userList 是一个 NSMutableDictionary,根据您发布的参考链接,我认为它是一个适合写入 plistFile 的对象。即使我将它放在 NSDictionary 中,然后尝试将其写入文件,它也不起作用。
    • 可能是路径,您对文件或路径中没有权限。您应用中的每个文件都可能受到定义的文件系统的影响。
    【解决方案2】:

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

    它会给你空对象,因为你的 plistPath 是 nil

    【讨论】:

    • if 语句(赋值)之后的 plistPath 不再是 nil,如果你看的话,我也在控制台中 NSLog-ing 它。
    • 当然,您的 infoDict 为 null,您无法为其设置值,如果为 null,请尝试分配它
    • @HuyNghia 谢谢!你是对的,我将 infoDict 设为 null,但现在我删除了以下行: NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];并将其替换为: NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] init];现在它是一个空字典而不是 null。但是写入问题仍然存在,它仍然返回一个否。
    • @justintime 你确定你的文件已经创建了吗 try BOOL Success = [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];检查
    • @Huy Nghia 我也使用查找器进行了检查,它正在创建中。
    【解决方案3】:
    NSMutableDictionary *fileData;
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
    
        if ([fileManager fileExistsAtPath: path])
        {
            fileData = [[NSMutableDictionary alloc] init];
            if ([fileData initWithContentsOfFile: path])
            {
                fileData = [fileData initWithContentsOfFile: path];
            }
    
        }
        else
        {
            [fileManager createFileAtPath:path contents:nil attributes:nil];
    
            fileData = [[NSMutableDictionary alloc] initWithCapacity:0];
    
        }
    
            [fileData setObject:data forKey:qid];
    
        BOOL wrote = [fileData writeToFile:path atomically:YES];
        NSLog(wrote ? @"yes, wrote to the file": @"no, didn't write to file");
    

    试试这个。为我工作。

    【讨论】:

    • 抱歉,您的代码对我不起作用,输出相同。但是您能否详细说明您认为代码中的不同之处,它们在我看来是一样的..
    • @justintime 您错过了fileData = [[NSMutableDictionary alloc] init];fileData = [[NSMutableDictionary alloc] initWithCapacity:0]; 这一行。似乎您的 Dictionary 对象根本没有被分配,因此始终为您提供 null 值。
    • 是的,我更正了。对不起,我会更新上面的代码。但是,我仍然无法写入文件。
    • 我的路径值是 /Users/admin/Library/Developer/CoreSimulator/Devices/7C881490-C50B-4B11-B9C3-8C2AF02B5A5D/data/Applications/365D6D67-38EE-4DFB- 8129-CF1A5B7E34C6/Documents/data.plist。我注意到您的路径中有一个额外的 /Containers/Data 。也许这就是你面临这个问题的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 2013-07-06
    相关资源
    最近更新 更多