简体   繁体   English

如何在iPhone中以编程方式在plist文件中添加多个数组

[英]how to add multiple arrays in plist file programmatically in iPhone

I want to create plist file with multiple array and array as Root type.I can add multiple array in dictionary but I want to add all array in one array programmatically.How is it possible to create plist file programmatically and write array data into it.Please guide me and give some sample links.Thanks. 我想用根类型创建具有多个数组和数组的plist文件。我可以在字典中添加多个数组,但是我想以编程方式将所有数组添加到一个数组中。如何以编程方式创建plist文件并将数组数据写入其中。请指导我,并提供一些示例链接。谢谢。 Here is my code to add array in dictionary : 这是我在字典中添加数组的代码:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Demo.plist"]; 

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) 
{
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Demo" ofType:@"plist"]; 

    [fileManager copyItemAtPath:bundle toPath: path error:&error];
}

firstArray = [[NSMutableArray alloc] initWithObjects:@"15-5-123",@"15-5-12",nil];
secondArray = [[NSMutableArray alloc] initWithObjects:@"TestFiling",@"TestFiling",nil];
thirdArray = [[NSMutableArray alloc] initWithObjects:@"15132561",@"15135601", nil];

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:firstArray forKey:@"firstArray"];
[dictionary setObject:secondArray forKey:@"secondArray"];
[dictionary setObject:thirdArray forKey:@"thirdArray"];

[dictionary writeToFile:path atomically:NO];

NSDictionary *dictionary1;
dictionary1 = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"dictionary1 %@",dictionary1);

this code is working fine for dictionary but i want to add arrays in array. 此代码对于字典工作正常,但我想在数组中添加数组。

The first section of your code is fine, you just need to switch the second part to using an array: 代码的第一部分很好,您只需要将第二部分切换为使用数组即可:

NSMutableArray *array = [[NSMutableDictionary alloc] init];
[array addObject:firstArray];
[array addObject:secondArray];
[array addObject:thirdArray];

[array writeToFile:path atomically:NO];

NSArray *array1 = [NSArray arrayWithContentsOfFile:path];
NSLog(@"array1 %@", array1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM