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

查看:173
本文介绍了在我的应用程序包中编写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备份;或未备份的caches目录。如果您不需要备份数据,请将其放在缓存中,这样就不会降低同步速度。你可以得到如下的目录路径:

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天全站免登陆