objective-c - .plist 文件写入失败!这段代码有什么问题?

标签 objective-c ios cocoa plist nsmutabledictionary

此代码后我的 plist 文件中没有任何内容。我的代码有什么问题?

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"aFile.plist"];
NSMutableDictionary *reqData = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
/*
some modifications to "reqData"
*/
[reqData writeToFile:finalPath atomically:YES];

文件中没有任何内容。可能是什么问题?

最佳答案

你最好写到Document文件夹:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask,
                                                     YES);
NSString *path = [paths objectAtIndex:0];

更新

根据 NSDictionary Class Reference :

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

在你的整个字典中是否有任何类型不是这些有效类型的对象?

关于objective-c - .plist 文件写入失败!这段代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10347680/

相关文章:

objective-c - cocoa 爷爷

ios - 决定使用核心数据或 NSUserDefaults

objective-c - 通过 UISplitViewController 的 iOS 7 导航栏显示褪色的灰色 View

ios - Objective-C类别未链接

ios - 低质量 iOS 屏幕截图

objective-c - 如何在 Objective-C 中制作可用蓝牙设备的下拉列表?

objective-c - 找不到 'UIWebViewDelegate' 的协议(protocol)声明

objective-c - 没有简单的划分

iphone - 我如何延迟初始化 NSMutableArray?

ios - 具有安全选项的 UITextField 的 NSAttributedString 背景颜色