iOS:将两个NSMutableArray存储在.plist文件中 [英] iOS: store two NSMutableArray in a .plist file

查看:211
本文介绍了iOS:将两个NSMutableArray存储在.plist文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储两个我在AppDelegate中用作全局数组的NSMutableArray。这两个数组也与NSUserDefaults一起存储。现在我想知道如何创建这个文件,以及每次修改它们时如何存储这两个数组。你能帮帮我吗?

I want to store two NSMutableArray that I use as global array in AppDelegate. These two array are also store with NSUserDefaults. Now I want to know how I must create this file and how can I store these two array everytime I modify them. Can You help me?

推荐答案


  1. 创建一个包含两个NSMutableArrays的NSArray。

  1. Create an NSArray containing your two NSMutableArrays.

NSArray *array = [NSArray arrayWithObjects:<#(id), ...#>, nil];


  • 将数组写入文件。

  • Write the array to a file.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSString *location = [libraryDirectory stringByAppendingString:@"/somefilename.plist"];
    [array writeToFile:location atomically:YES];
    


  • 从文件加载数组。

  • Load the array from the file.

    NSString *path = [bundle pathForResource:@"file" ofType:@"plist"];
    NSArry *array = (path != nil ? [NSArray arrayWithContentsOfFile:location] : nil);
    


  • 这篇关于iOS:将两个NSMutableArray存储在.plist文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆