我想保存多个 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) 加载数据,那么从服务器下载的数据未保存。