I have a plist and I copy the plist to my xcode project but it seems like the file is not in bundle:
我有一个plist,我把plist复制到我的xcode项目中,但是看起来这个文件不是捆绑在一起的:
NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
nslog (@"dict %@",dict);
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?
但是文件plist文件没有显示。问题是,如何将文件添加到项目中以查看包中的内容?你们中有人知道如何修改plist并保存更改吗?
I'll really appreciated your help
我真的很感激你的帮助
P.S. I'm using Xcode 5.
附注:我使用的是Xcode 5。
3 个解决方案
#1
3
You can create a plist file in your bundle and modify its contents as:
您可以在您的bundle中创建一个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);
#2
2
infoDictionary
will return Info.plist file. You should use pathForResource:ofType:
method of NSBundle
class.
infoDictionary将返回信息。plist文件。您应该使用pathForResource:ofType: NSBundle类的方法。
NSString *commonDictionaryPath;
if (commonDictionaryPath = [[NSBundle mainBundle] pathForResource:@"CommonDictionary" ofType:@"plist"]) {
theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
}
How can I add plist to bundle
Drag your plist to "Copy Bundle Resources" phase of your build target
如何将plist添加到bundle将plist拖到构建目标的“复制bundle Resources”阶段
modify the plist
Copy the plist to your app's Documents folder and modify it there.
修改plist,将plist复制到你的应用程序的文档文件夹,并在那里修改它。
#3
0
this should work
这应该工作
NSString*pListDictPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist" inDirectory:@"YourDirectory"];
if()
{
NSDictionary *theDictionary = [[NSDictionary alloc] initWithContentsOfFile:pListDictPath ];
}
#1
3
You can create a plist file in your bundle and modify its contents as:
您可以在您的bundle中创建一个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);
#2
2
infoDictionary
will return Info.plist file. You should use pathForResource:ofType:
method of NSBundle
class.
infoDictionary将返回信息。plist文件。您应该使用pathForResource:ofType: NSBundle类的方法。
NSString *commonDictionaryPath;
if (commonDictionaryPath = [[NSBundle mainBundle] pathForResource:@"CommonDictionary" ofType:@"plist"]) {
theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
}
How can I add plist to bundle
Drag your plist to "Copy Bundle Resources" phase of your build target
如何将plist添加到bundle将plist拖到构建目标的“复制bundle Resources”阶段
modify the plist
Copy the plist to your app's Documents folder and modify it there.
修改plist,将plist复制到你的应用程序的文档文件夹,并在那里修改它。
#3
0
this should work
这应该工作
NSString*pListDictPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist" inDirectory:@"YourDirectory"];
if()
{
NSDictionary *theDictionary = [[NSDictionary alloc] initWithContentsOfFile:pListDictPath ];
}