3

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

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

但文件 plist 文件未显示。问题,如何以在包中查看的方式将文件添加到项目中?你们中的任何人都知道如何修改 plist 并保存更改吗?

我非常感谢你的帮助

PS我正在使用Xcode 5。

4

3 回答 3

4

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

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);
于 2013-10-28T10:37:42.950 回答
2

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

NSString *commonDictionaryPath;
if (commonDictionaryPath = [[NSBundle mainBundle] pathForResource:@"CommonDictionary" ofType:@"plist"])  {
    theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
}

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

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

于 2013-10-27T09:41:23.220 回答
0

这应该工作

     NSString*pListDictPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist" inDirectory:@"YourDirectory"];
     if()
     {
            NSDictionary *theDictionary = [[NSDictionary alloc] initWithContentsOfFile:pListDictPath ];
     }
于 2013-10-27T06:31:34.947 回答