在viewDidLoad
,我的应用程序填充的NSMutableArray arrayOfHaiku
,这是NSDictionary
项的数组,如下所示:当使用新手询问读取的NSMutableArray和保存的NSMutableArray到文档文件
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"haiku.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"haiku" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
self.arrayOfHaiku = [[NSMutableArray alloc] initWithContentsOfFile: path];
的应用,用户将项目添加到self.arrayOfHaiku
。截至目前,我包括在viewDidUnload
下面的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"haiku.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: path])
{
[self.arrayOfHaiku writeToFile:path atomically:YES];
}
这里是我的问题:
难道我对
viewDidUnload
代码),只需在文件存储在文件夹中的阵列写(也就是,在viewDidLoad
中访问的数组)与新的扩展数组(这是我想要做的),或者它b)将新的扩展数组追加到已存储在Documents文件夹中的数组中,以便最终数组的长度是一串重复的两倍多?如果答案是“b)”,那么我需要改变做“a)”?
更好的方法是以这种方式做到这一点,在
viewDidUnload
,或b)每当用户添加项目到self.arrayOfHaiku时,展开文档文件夹中的数组?如果答案是“b)”,那么我需要改变做“b)”?
感谢您的帮助。
非常非常感谢!数据结构非常简单('self.arrayOfHaiku'是'NSDictionary'项目的数组,每个项目包含两个对象和两个键),self.arrayOfHaiku不可能包含超过200或300个项目,所以我我会在几个地方尝试它 - 看看每次做它需要多少成本,或者它是否会影响到将它放在其他地方 - 但我没有想过使用viewDidUnload来解决这个问题,所以无论如何我会为它找到一个新家。非常非常感谢你! – 2012-08-13 19:59:02