0

我想保存多个 NSMutableArray 并加载它,因为这个数组从服务器获取它的内容,我不想每次打开应用程序时都重新加载该数据。首先我声明了路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *firstPath = [documentsDirectory stringByAppendingPathComponent:@"first"];
     NSString *secondPath = [documentsDirectory stringByAppendingPathComponent:@"second"];
     NSString *thirdPath = [documentsDirectory stringByAppendingPathComponent:@"third"];
     NSString *fourthPath = [documentsDirectory stringByAppendingPathComponent:@"fourth"];

然后保存 NSMutableArrays:

         [firstArray writeToFile:firstPath atomically:YES];
         [secondArray writeToFile:secondPath atomically:YES];
         [thirdArray writeToFile:thirdPath atomically:YES];
         [fourthArray writeToFile: fourthPath atomically:YES];

然后在其他 NSMutableArrays 中打开这些文件:

    firstArrayget = [NSMutableArray arrayWithContentsOfFile:firstPath];
    secondArrayget = [NSMutableArray arrayWithContentsOfFile:secondPath];
    thirdArrayget = [NSMutableArray arrayWithContentsOfFile:thirdPath];
    fourthArrayget = [NSMutableArray arrayWithContentsOfFile:fourthPath];

然后我尝试将这些数组(...Arrayget 即 firstArrayget)加载到 TableView 中。

数据被加载到 TableView 中,但是当我向下滚动时,应用程序崩溃并出现错误:

*** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x930fc80

并在文件中:

Thread 1:EXC_BREAKPOINT(code=EXC_1386_BPT,subcode=0x0)

但是如果我说 TableView 从 (...Array ie firstArray) 加载数据,那么从服务器下载的数据未保存。

4

1 回答 1

0

假设您使用的是 MRC 而不是 ARC:
看起来您正在将 ivar 设置为 autoreleased NSMutableArray
尝试调用s,否则当自动释放池耗尽时,您retain的s 将被释放并因此被释放。另一种解决方案是为您的每个s 使用一个属性,如下所示: NSMutableArrayNSMutableArrayNSMutableArray

// Create a property in your header file
@property (retain) NSMutableArray *firstArrayget;

// And set the property in your method
[self setFirstArrayGet:[NSMutableArray arrayWithContentsOfFile:firstPath]];

您可以在https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html找到有关 Objective-C 内存管理的更多信息

于 2013-03-11T19:09:40.280 回答