【问题标题】:How can I add the following lines to my plist file?如何将以下行添加到我的 plist 文件中?
【发布时间】:2013-01-26 08:43:28
【问题描述】:

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

【问题讨论】:

  • 不清楚你想添加什么以及你得到了什么。
  • 我编辑了我的问题。再看一遍
  • 在执行此代码时出现错误?似乎没问题,顺便说一句,文件@"/Users/me/Desktop/Info2.plist" 存在吗?如果不存在,您的plist 变量将是null,它们什么都不会发生
  • 我没有收到任何错误。我收到错误的输出。我没有收到嵌套标签。我只想知道如何将这些标签添加到我的 plist。

标签: iphone ios xcode jailbreak springboard


【解决方案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。

【讨论】:

  • 如果您看到我的要求。字符串标签必须嵌套在数组标签中。我只是想知道如何实现这一点?我想将给定的 xml 添加到一个已经存在的 info.plist 文件中,但它非常混乱。告诉我如何实现?
  • 您正在做的是正确的嵌套标签,但是您无法将文件保存在应用程序的Documents 文件夹之外,因此您无法将文件保存在桌面上。尝试使用我的代码的第二部分将其保存在该文件夹中。
  • 它是越狱设备,我可以保存在任何我想要的地方:)
  • 嗯...然后试试 NSLog 看看你得到了什么。
【解决方案2】:

您需要设置一个字符串数组作为键 @"AppTag" 的对象,如下所示:

NSArray *strings = @[@"hidden"];
plist[@"AppTag"] = strings;

【讨论】:

    【解决方案3】:

    你可以试试下面的代码

    -(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];
    

    希望这会有所帮助.. 快乐编码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 2012-07-17
      • 2011-12-25
      • 1970-01-01
      相关资源
      最近更新 更多