在我的应用程序包中将 NSDictionary 写入 plist [英] writing NSDictionary to plist in my app bundle

查看:23
本文介绍了在我的应用程序包中将 NSDictionary 写入 plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 NSDictionary 写入 plist,但是当我打开 plist 时,没有数据写入其中.从日志中我的路径看起来是正确的,我的代码非常标准.有什么想法吗?

I'm trying to write an NSDictionary to a plist but when I open the plist no data has been written to it. From the log my path looks correct and my code is pretty standard. Any ideas?

NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

for (id key in dictionary) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

NSString *path = [[NSBundle mainBundle] pathForResource:@"FormData" ofType:@"plist"];
NSLog(@"path:%@", path);

[dictionary writeToFile:path atomically:YES];

推荐答案

Johan 是对的 -- 你不能修改你的应用程序包.您有两个很好的选择来保存字典:文档目录,当用户同步他们的设备时由 iTunes 备份;或未备份的缓存目录.如果您不需要备份数据,请将其放入缓存中,以免减慢同步速度.您可以像这样获取目录路径:

Johan is right -- you're not allowed to modify your app's bundle. You have two good options for where to save your dictionary: the documents directory, which is backed up by iTunes when a user syncs their device; or the caches directory, which is not backed up. If you don't need to have the data backed up, put it in caches so you don't slow down syncing. You can get the directory paths like so:

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

NSString *cachesDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

这篇关于在我的应用程序包中将 NSDictionary 写入 plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆