【问题标题】:How to add new Key to nested plist如何将新密钥添加到嵌套 plist
【发布时间】:2014-06-18 11:01:59
【问题描述】:

我的应用中有自定义 plist,我想在嵌套级别添加新键。

如何添加?以编程方式?

这是我的 plist 文件的结构: : 如何做到这一点?

请提前帮助和感谢。

【问题讨论】:

  • 按下+按钮?
  • 目前有什么问题?为什么不能添加?您想像@trojanfoe 提到的那样编辑文件,还是想在整个代码中以编程方式进行编辑?
  • 我想以编程方式添加它

标签: ios objective-c plist


【解决方案1】:

您不能在 App-Bundle 中编辑文件。您必须将其作为NSMutableDictionary 读入,更改并保存到您的文档文件夹中。

/*--- get bundle file      ---*/
NSString *path = [[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"];
NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

/*--- set value with key   ---*/
[rootDict setValue:@"NewKeyContent" forKeyPath:@"Mobiles.Brands.TheNewKey"];

/*--- get documents path   ---*/
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES) lastObject];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Products.plist"];

/*--- save file            ---*/
[rootDict writeToFile:writablePath atomically: YES];

之后,您必须从 Documents 目录中打开它,否则您将始终从零开始。

/*--- get documents file   ---*/
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"Products.plist"];
NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

/*--- set value with key   ---*/
[rootDict setValue:@"NewKeyContent" forKeyPath:@"Mobiles.Brands.TheNewKey"];

/*--- get bundle file      ---*/
[rootDict writeToFile:writablePath atomically: YES];

【讨论】:

  • 当我写这个 Mobiles.Brands.TheNewKey 键时添加在 Mobiles 下而不是 Mobiles.Brands 下
  • 对不起。它的setValue:forKeyPath:。我修复了分析器代码。
  • 很棒,伙计,但如果我想检查 plist 中是否存在该键怎么办。为此我应该使用什么语法?
  • if ([rootDict valueForKeyPath:@"Mobiles.Brands.TheNewKey"]){NSLog(@"exists");} else {NSLog(@"missing");};
  • 谢谢,但您可能想裁剪该图像并修复底部文本。
【解决方案2】:

使用 NSMutableDictionary + (id /* NSDictionary * */)dictionaryWithContentsOfFile:(NSString *)path 创建字典。并使用 [字典 setValue:@"new_value" forKeyPath:@"Mobiles.Brands.new_key"];

【讨论】:

    【解决方案3】:

    正如其他人所说,您应该通过单击“+”按钮在 plist 编辑器中添加新键。

    如果您想在代码中编辑您的 plist,则要复杂得多。

    plist 文件被读入内存,其所有对象都是不可变的。您需要为要添加的对象创建整个分支的可变副本。像这样的:

    //Make a mutable copy of the outermost dictionary
    NSMutableDictionary *outer = [startingOuterDict mutableCopy];
    
    //Create a mutable copy of the dictionary at key "Mobiles" and replace it in "outer"
    NSMutableDictionary *mobiles =  [outer[@"Mobiles"] mutableCopy];
    outer[@"Mobiles"] = mobiles;
    
    //Create a mutable copy of the dictionary at key "Brands" and replace it in "mobiles"
    NSMutableDictionary *brands =  [mobiles[@"Brands"] mutableCopy];
    mobiles[@"Brands"] = brands;
    
    //Finally, add a new key in the "Brands" dictionary
    brands[@"newKey"] = @"Apple";
    

    虽然很棘手,但可以编写一个“mutableDeepCopy”方法,将字典、数组和集合的对象图中的所有容器转换为它们的可变等价物。但是,在这种情况下,您不需要它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 2021-02-23
      • 2016-06-14
      • 2018-11-07
      • 1970-01-01
      • 2015-07-27
      相关资源
      最近更新 更多