Objective c Mac OSX如何将plist添加到bundle并修改plist
我有一个plist,我将plist复制到我的xcode项目中,但该文件似乎不在捆绑包中:Objective c Mac OSX如何将plist添加到bundle并修改plist,objective-c,xcode,macos,Objective C,Xcode,Macos,我有一个plist,我将plist复制到我的xcode项目中,但该文件似乎不在捆绑包中: NSDictionary *dict = [[NSBundle mainBundle] infoDictionary]; nslog (@"dict %@",dict); 但是plist文件没有显示。问题,如何将文件添加到项目中,以便在捆绑包中查看?还有谁知道如何修改plist并保存更改 我真的很感激你的帮助 另外,我使用的是Xcode 5。应该可以 NSString*pListDictPath
NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
nslog (@"dict %@",dict);
但是plist文件没有显示。问题,如何将文件添加到项目中,以便在捆绑包中查看?还有谁知道如何修改plist并保存更改
我真的很感激你的帮助
另外,我使用的是Xcode 5。应该可以
NSString*pListDictPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist" inDirectory:@"YourDirectory"];
if()
{
NSDictionary *theDictionary = [[NSDictionary alloc] initWithContentsOfFile:pListDictPath ];
}
infoDictionary
将返回Info.plist文件。
您应该使用NSBundle
类的pathForResource:ofType:
方法
NSString *commonDictionaryPath;
if (commonDictionaryPath = [[NSBundle mainBundle] pathForResource:@"CommonDictionary" ofType:@"plist"]) {
theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
}
如何将plist添加到bundle将plist拖到构建目标的“复制捆绑资源”阶段 修改plist
将plist复制到应用程序的Documents文件夹中并在其中进行修改。您可以在捆绑包中创建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);
是否要在捆绑包中写入plist文件?是否询问如何通过编程修改plist?