首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何添加plist来打包和修改plist

如何添加plist来打包和修改plist
EN

Stack Overflow用户
提问于 2013-10-27 06:20:38
回答 3查看 2K关注 0票数 3

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

代码语言:javascript
复制
NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
nslog (@"dict %@",dict);

但是文件plist文件没有显示。问题是,如何将文件添加到项目中,以查看包中的内容?另外,你们中的任何人都知道我如何修改plist并保存更改?

我真的很感激你的帮助

我用的是Xcode 5。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-28 10:37:42

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

代码语言:javascript
复制
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);
票数 4
EN

Stack Overflow用户

发布于 2013-10-27 09:41:23

infoDictionary将返回Info.plist文件。您应该使用pathForResource:ofType:方法的NSBundle类。

代码语言:javascript
复制
NSString *commonDictionaryPath;
if (commonDictionaryPath = [[NSBundle mainBundle] pathForResource:@"CommonDictionary" ofType:@"plist"])  {
    theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
}

如何将plist添加到包中?

将plist拖到构建目标的“复制捆绑资源”阶段

修改plist

将plist复制到应用程序的Documents文件夹,并在那里修改它。

票数 2
EN

Stack Overflow用户

发布于 2013-10-27 06:31:34

这应该能行

代码语言:javascript
复制
     NSString*pListDictPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist" inDirectory:@"YourDirectory"];
     if()
     {
            NSDictionary *theDictionary = [[NSDictionary alloc] initWithContentsOfFile:pListDictPath ];
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19615248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档