Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何向嵌套的plist添加新键_Ios_Objective C_Plist - Fatal编程技术网

Ios 如何向嵌套的plist添加新键

Ios 如何向嵌套的plist添加新键,ios,objective-c,plist,Ios,Objective C,Plist,我的应用程序中有自定义plist,我想在嵌套级别添加新密钥 我如何添加它?在程序上 以下是我的plist文件的结构: : 如何做到这一点 请提前提供帮助和感谢。使用NSMutableDictionary+(id/*NSDictionary**/)dictionaryWithContentsOfFile:(NSString*)路径创建字典。和使用 [dictionary setValue:@“new_value”forKeyPath:@“Mobiles.Brands.new_key”] 您无法编辑

我的应用程序中有自定义plist,我想在嵌套级别添加新密钥

我如何添加它?在程序上

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

请提前提供帮助和感谢。

使用NSMutableDictionary+(id/*NSDictionary**/)dictionaryWithContentsOfFile:(NSString*)路径创建字典。和使用
[dictionary setValue:@“new_value”forKeyPath:@“Mobiles.Brands.new_key”]

您无法编辑应用程序包中的文件。您必须将其作为
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];

正如其他人所说,只需单击“+”按钮在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”方法,将字典、数组和集合的对象图中的所有容器转换为它们的可变等价物是可能的,尽管这很棘手。但是,在这种情况下,您不需要它。

按下
+
按钮?目前有什么问题?你为什么不能加上它?您想按照上面提到的@特洛伊木马编辑该文件,还是想在整个代码中以编程方式进行编辑?我想在编写此
Mobiles.Brands时以编程方式添加它。newkey
键添加在Mobiles下,而不是Mobiles下。Brands对不起。其
设置值:forKeyPath:
。我修复了anser代码。非常好,伙计,但是如果我想检查plist中是否存在该键呢。为此,我应该使用什么语法?
if([rootDict valueForKeyPath:@“Mobiles.Brands.TheNewKey”]){NSLog(@“exists”);}else{NSLog(@“missing”);}谢谢,但你可能想裁剪该图像并修复底部文本。