Mac OSX如何添加plist进行捆绑和修改plist [英] Mac OSX How can I add plist to bundle and modify the plist

查看:125
本文介绍了Mac OSX如何添加plist进行捆绑和修改plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个plist,然后将plist复制到我的xcode项目中,但似乎该文件不在捆绑包中:

I have a plist and I copy the plist to my xcode project but it seems like the file is not in bundle:

NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
nslog (@"dict %@",dict);

,但未显示文件plist文件.问题,如何将文件添加到项目中才能在捆绑包中看到?你们当中的任何人都知道我该如何修改plist并保存更改?

but the file plist file is not showing. Question, how can I add the file to project in a way to see in the bundle? also any of you knows how can I modify the plist and save the changes?

非常感谢您的帮助

P.S.我正在使用Xcode 5.

P.S. I'm using Xcode 5.

推荐答案

您可以在您的 bundle 中创建一个 plist 文件并将其内容修改为:

You can create a plist file in your bundle and modify its contents as:

NSString *bundlePath = [[NSBundle mainBundle]bundlePath]; //Path of your bundle
NSString *path = [bundlePath stringByAppendingPathComponent:@"MyPlist.plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) 
{
    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];  // if file exist at path initialise your dictionary with its data
}
else
{
    // If the file doesn’t exist, create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

//To insert the data into the plist
int value = 5;
[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[data writeToFile: path atomically:YES];
[data release];

//To retrieve the data from the plist
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
int savedValue;
savedValue = [[savedData objectForKey:@"value"] intValue];
NSLog(@"%i",savedValue);

这篇关于Mac OSX如何添加plist进行捆绑和修改plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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