无法将NSDictionary写入plist文件
更新:当我更改以下行时,我可以写入文件
[infoDict setObject:userList forKey:@"users"];
至
[infoDict setObject:@"sampleString" forKey:@"users"];
所以,我试图写入文件的结构一定有问题,有什么想法?
我正在尝试创build一个plist文件,并在运行时在我的iOS应用程序中写入。 我不能写信给它,但我可以阅读。 writeToFile方法返回一个“NO”。 当我手动尝试手动打开我使用Finder创build的文件时,它会显示“无法读取数据,因为它的格式不正确”,这是因为在使用文本编辑器打开文件时,它是空,并且不显示plisttypes的文件应该具有的任何xml标签。 而且我知道我可以从中读取,因为在手动拖动文件夹中的“有效”plist文件时,我的代码能够从那里读取一个空列表。
1)为什么不创build一个有效的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] --------------------------------------------
您可以在userList中检查您的数据,它必须是NSMutableDictionary Reference中的属性列表对象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary的实例),doc
问题是在行NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
它会给你null对象,因为你的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");
尝试这个。 为我工作。