0

我看过很多关于添加到 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];
4

3 回答 3

2

您需要将字符串数组设置为 key 的对象@"AppTag",如下所示:

NSArray *strings = @[@"hidden"];
plist[@"AppTag"] = strings;
于 2013-02-11T09:29:01.310 回答
1

您需要做的是添加strings到您的array,然后添加arraydictionary文件的。在以下示例中,我将一些单词及其定义添加到名为 的文件中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。

于 2013-02-11T09:25:23.467 回答
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];

希望这会有所帮助.. 快乐编码</p>

于 2013-02-11T09:46:07.720 回答