【问题标题】:how to add multiple arrays in plist file programmatically in iPhone如何在 iPhone 中以编程方式在 plist 文件中添加多个数组
【发布时间】:2015-06-16 06:48:20
【问题描述】:

我想创建具有多个数组和数组作为根类型的 plist 文件。我可以在字典中添加多个数组,但我想以编程方式将所有数组添加到一个数组中。如何以编程方式创建 plist 文件并写入数组数据进入它。请指导我并提供一些示例链接。谢谢。 这是我在字典中添加数组的代码:

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);

这段代码在字典中运行良好,但我想在数组中添加数组。

【问题讨论】:

  • 您说您可以在字典中添加数组,显示该代码以及您如何尝试为数组数组修改它
  • 在这里你的How to Answer[1] 关注这个东西。 [1]:stackoverflow.com/questions/3636524/…
  • @Wain - 请查看我编辑的问题。我已经添加了我的代码。

标签: ios objective-c iphone nsmutablearray plist


【解决方案1】:

您的代码的第一部分很好,您只需将第二部分切换为使用数组:

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多