无法将NSDictionary写入plist文件

时间:2014-10-16 09:18:07

标签: ios objective-c xcode nsdictionary plist

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

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

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

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


我正在尝试创建一个plist文件,并在我的iOS应用程序中在运行时写入它。我无法写信但我能阅读。 writeToFile方法返回'NO'。 当我手动尝试手动打开我使用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] --------------------------------------------

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

由于plistPath为零

,它会为您提供空对象

答案 2 :(得分:0)

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

试试这个。为我工作。