我已经看过很多关于添加到plist文件的教程,但我似乎无法正确地将嵌套标签插入到plist文件中。 我想将以下行添加到我的Info.plist文件
<key>AppTag</key> <array>
<string>hidden</string> </array>
NSMutableDictionary *plist = [[NSDictionary dictionaryWithContentsOfFile:@"/Users/me/Desktop/Info2.plist"] mutableCopy];
[plist setObject:@"AppTag" forKey:@"key"];
[plist setObject:@"hidden" forKey:@"string"];
[plist writeToFile:@"/Users/me/Desktop/Info2.plist" atomically:YES];
[plist release];
答案 0 :(得分:2)
您需要将字符串数组设置为键@"AppTag"
的对象,如下所示:
NSArray *strings = @[@"hidden"];
plist[@"AppTag"] = strings;
答案 1 :(得分:1)
您需要做的是将strings
添加到array
,然后array
添加到文件的dictionary
。在以下示例中,我将一些单词及其定义添加到名为WordList.plist
的文件中:
NSArray *values = [[NSArray alloc] initWithObjects:word.name, word.definition, nil];
NSArray *keys = [[NSArray alloc] initWithObjects: NAME_KEY, DEFINITION_KEY, nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:values forKeys:keys];
[wordsListArray addObject:dict];
NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject];
destinationPath = [destinationPath stringByAppendingPathComponent:@"WordList.plist"];
[wordsListArray writeToFile:destinationPath atomically:YES];
我希望这会有所帮助。
另外,我认为这是不能正常工作的部分:@"/Users/me/Desktop/Info2.plist"
尝试NSLogging。
答案 2 :(得分:1)
您可以尝试以下代码
-(NSString *) datafilepath{
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents=[path objectAtIndex:0];
return [documents stringByAppendingFormat:@"/Info2.plist"]; // you can change the path to desktop
}
在ViewDidLoad或任何方法
下NSString *filepath3=[self datafilepath];
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath3]) {
pricedict=[[NSMutableDictionary alloc]initWithContentsOfFile:filepath3];
}
[pricedict setObject:@"AppTag" forKey:@"key"];
[pricedict setObject:@"hidden" forKey:@"string"];
[pricedict writeToFile:pngfilepath atomically:YES];
希望这有帮助..快乐编码