如何写入plist文件?
问题描述:
我写了一小段代码写入plist文件。我没有收到XCode的任何错误或警告,所以我真的迷失了。在这段代码中没有定义“Root”,但它只是从plist中提取的数据字典。如何写入plist文件?
有没有人有一个想法,为什么这不会写入plist文件?它从字面上根本不会改变文件。
NSMutableArray *newReports = [[NSMutableArray alloc] init];
newReports = [[Root objectForKey:@"Reports"] mutableCopy ];
//Creating data model for new report
NSMutableDictionary *newReport = [[NSMutableDictionary alloc] init];
NSString *tempTitle = titleField.text;
NSString *tempEmployee = employeeField.text;
NSArray *tempNodes = [[NSArray alloc] init];
[newReport setObject:tempTitle forKey:@"Title"];
[newReport setObject:tempEmployee forKey:@"Employee"];
[newReport setObject:tempNodes forKey:@"Nodes"];
[newReports addObject:newReport];
NSMutableDictionary *newRoot = [[NSMutableDictionary alloc] init];
[newRoot setObject:newReports forKey:@"Reports"];
//Writing data to plist
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *filePath = [Path stringByAppendingPathComponent:@"data.plist"];
[newRoot writeToFile:filePath atomically: YES];
感谢您的帮助!
答
试图构建这样
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath
stringByAppendingPathComponent:@"data.plist"];
[newRoot writeToFile:filePath atomically: YES];
答
如果您在设备上运行此功能,则主包是只读的。您需要将此plist编写到用户的文档目录中。
感谢您的帮助文件的路径,但我认为这个问题是关系到构建newReport - 它原来是一个零值。 – Andrew 2010-11-07 03:20:36