我有一个plist,我将这个plist复制到我的xcode项目中,但是文件似乎不在捆绑中:
NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
nslog (@"dict %@",dict);但是文件plist文件没有显示。问题是,如何将文件添加到项目中,以查看包中的内容?另外,你们中的任何人都知道我如何修改plist并保存更改?
我真的很感激你的帮助
我用的是Xcode 5。
发布于 2013-10-28 10:37:42
您可以在包中创建一个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-27 09:41:23
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-27 06:31:34
这应该能行
NSString*pListDictPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist" inDirectory:@"YourDirectory"];
if()
{
NSDictionary *theDictionary = [[NSDictionary alloc] initWithContentsOfFile:pListDictPath ];
}https://stackoverflow.com/questions/19615248
复制相似问题