Ios 无法将NSDictionary写入plist文件

Ios 无法将NSDictionary写入plist文件,ios,objective-c,xcode,nsdictionary,plist,Ios,Objective C,Xcode,Nsdictionary,Plist,更新:当我更改以下行时,我能够写入文件 [infoDict setObject:userList forKey:@"users"]; 到 所以,我试图写入文件的结构肯定有问题,有什么想法吗 我正在尝试创建一个plist文件,并在运行时在iOS应用程序中写入该文件。我不能给它写信,但我能读。writeToFile方法返回“否”。 当我手动尝试手动打开使用Finder创建的文件时,它会说“无法读取数据,因为它的格式不正确”。我理解这一点,因为使用文本编辑器打开文件时,它是空的,并且不会显示pli

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

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

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


我正在尝试创建一个plist文件,并在运行时在iOS应用程序中写入该文件。我不能给它写信,但我能读。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] --------------------------------------------
2014-10-17 10:50:11.290 UserReggy[1228:10032]*将内容写入plist文件*
2014-10-17 10:50:11.291 UserReggy[1228:10032]是,文件存在
2014-10-17 10:50:11.291 UserReggy[1228:10032]文件路径:/Users/rvyas1/Library/Developer/CoreSimulator/Devices/DB612284-F481-467A-BAE8-37750497F42A/data/Containers/data/Application/C9620C4D-EF1F-4C17-8D28-4e9b4b41f2c2/Documents/Users.plist
2014-10-17 10:50:11.291用户注册信息:{
}
2014-10-17 10:50:11.291用户注册信息:{
用户={
dfgdfgd=“”;
};
}
2014-10-17 10:50:11.292 UserReggy[1228:10032]不,没有写入文件
2014-10-17 10:50:11.294用户注册[1228:10032]--------------------------------------------

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

问题出在第行
NSMutableDictionary*infoDict=[NSMutableDictionary Dictionary WithContentsOfFile:plistPath]

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

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

试试这个。为我工作。

在if语句(赋值)之后,plistPath不再是nil,如果你看的话,我也会在控制台中记录它。当然,你的infoDict是null,你不能为它设置值。如果null@HuyNghia谢谢你是对的,我将infoDict设置为null,但现在我删除了以下行:NSMutableDictionary*infoDict=[NSMutableDictionary dictionarywhithcontentsoffile:plistPath];并将其替换为:NSMutableDictionary*infoDict=[[NSMutableDictionary alloc]init];现在它是一个空字典而不是空字典。但是写入问题仍然存在,它仍然返回一个编号。@justintime您确定您的文件已经创建了try BOOL Success=[[NSFileManager defaultManager]createFileAtPath:filePath contents:nil attributes:nil];到check@HuyNghia我也使用finder进行了检查,它正在创建中。对不起,您的代码对我不起作用,输出相同。。但是你能详细说明一下你认为你的代码有什么不同吗,它们在我看来是一样的。@justintime你错过了这行
fileData=[[NSMutableDictionary alloc]init]
文件数据=[[NSMutableDictionary alloc]initWithCapacity:0]。似乎根本没有分配您的Dictionary对象,因此始终为您提供空值。是的,我更正了这一点。对不起,我会更新上面的代码。但是,我仍然无法写入该文件。我的路径值为/Users/admin/Library/Developer/CoreSimulator/Devices/7C881490-C50B-4B11-B9C3-8C2AF02B5A5D/data/Applications/365D6D67-38EE-4DFB-8129-CF1A5B7E34C6/Documents/data.plist。我注意到您的路径中有一个额外的/Containers/Data。也许这就是你面临这个问题的原因。userList是一个NSMutableDictionary,根据你发布的参考链接,我认为它是一个适合在plistFile中编写的对象。即使我将其放入NSDictionary,然后尝试将其写入文件,它也无法工作。可能是路径,您没有对该文件或路径的权限。您应用程序中的每个文件都可能受到定义的文件系统的影响。
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");