我在整个程序中调用函数 archiveQueue 来存储类型为 MYJSON 的对象,现在我想恢复我迄今为止存储在数组中的所有内容。
以下是我用来存储对象的函数:
- (void)archiveQueue:(MYJSON *)msg{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:msg];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *file = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myqueue.txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:file contents:data attributes:nil];
NSLog(@"ArchiveQueue: file = %@", file);
}
以下是我用来恢复对象的功能。
- (void)restoreQueue {
NSLog(@"RestoreQueue: Restoring queue from Document Directory.");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *file = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"myqueue.txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *data = [fileManager contentsAtPath:file];
// How to unarchive that array of objects being stored.
}
我想知道我应该如何解压缩存储为对象数组的所有内容?