2013-10-27 39 views
3

我有一个plist中,我的plist中复制到我的Xcode项目,但它似乎是一个文件不在包:的Mac OSX如何添加的plist捆绑和修改的plist

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

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

我真的很感激你的帮助

p.S.我使用的Xcode 5

+0

你想要写在你的包plist文件? –

+0

你问的如何编程修改plist? – markhunte

回答

3

您可以创建组合中的一个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); 
0

这应该工作

 NSString*pListDictPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist" inDirectory:@"YourDirectory"]; 
    if() 
    { 
      NSDictionary *theDictionary = [[NSDictionary alloc] initWithContentsOfFile:pListDictPath ]; 
    } 
2

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

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

我如何添加的plist捆绑
将您的plist为“复制包资源”阶段的构建目标

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