2013-10-27 113 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到您的應用程序的文件文件夾並在那裏修改它。