- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 NSKeyedArchiver 时遇到了一个问题,这让我困惑了很长一段时间,而且似乎无法查明错误。
我有一个可变数组,由“设备”类的对象组成。在我的 appDelegate 中,我保留了一个 mutableArray of Devices 并且我具有以下三个功能:
- (void) loadDataFromDisk {
self.devices = [NSKeyedUnarchiver unarchiveObjectWithFile: self.docPath];
NSLog(@"Unarchiving");
}
- (void) saveDataToDisk {
NSLog(@"Archiving");
[NSKeyedArchiver archiveRootObject: self.devices toFile: self.docPath];
}
- (BOOL) createDataPath {
if (docPath == nil) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *docsDir = [paths objectAtIndex:0];
self.docPath = [docsDir stringByAppendingPathComponent: @"devices.dat"];
NSLog(@"Creating path");
}
NSLog(@"Checking path");
NSError *error;
BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath: docPath withIntermediateDirectories: YES attributes: nil error:&error];
if (!success) {
NSLog(@"Error creating data path: %@", [error localizedDescription]);
}
return success;
}
我一直从解档过程中得到一个空的 mutableArray。我正在使用 ARC,不确定这是否与它有任何关系。
最佳答案
显然,我不知道的是您必须先将根对象(在本例中为数组)保存到 NSMutableDictionnary。
NSMutableDictionary *rootObject;
rootObject = [NSMutableDictionary dictionary];
[rootObject setValue: self.devices forKey: @"devices"];
然后用 NSKeyedArchiver 保存 rootObject。很奇怪,在任何教程中都没有看到这一点。
因此,您最终得到以下用于将数据加载和保存到 NSKeyedArchiver 的函数。
- (void) loadArrayFromArchiver {
NSMutableDictionary *rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile: [self getDataPath]];
if ([rootObject valueForKey: @"devices"]) {
self.devices = [rootObject valueForKey: @"devices"];
}
NSLog(@"Unarchiving");
}
- (void) saveArrayToArchiver {
NSLog(@"Archiving");
NSMutableDictionary *rootObject = [NSMutableDictionary dictionary];
[rootObject setValue: self.devices forKey: @"devices"];
[NSKeyedArchiver archiveRootObject: rootObject toFile: [self getDataPath]];
}
- (NSString *) getDataPath {
self.path = @"~/data";
path = [path stringByExpandingTildeInPath];
NSLog(@"Creating path");
}
关于iOS NSKeyedArchiver unarchive 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18954529/
我在使用 NSKeyedArchiver 时遇到了一个问题,这让我困惑了很长一段时间,而且似乎无法查明错误。 我有一个可变数组,由“设备”类的对象组成。在我的 appDelegate 中,我保留了一个
我正在尝试在远程计算机中下载并解压缩一个 tar 存档,如果不存在,则必须创建远程目标。但它没有发生。 ERROR: destination directory doesn't exist 我的代码:
我正在编写一个 ansible playbook 来自动安装一个软件。当我从网站下载压缩包时,我有: software-package-release.tar.gz 解压后,我只剩下目录 softwa
我使用 Ansible 在 EC2 上运行 AmazonLinux2。但是,当执行 Unarchive 命令时,会显示以下错误。 "Failed to find handler for \"/tmp/
我使用 Ansible 在 EC2 上运行 AmazonLinux2。但是,当执行 Unarchive 命令时,会显示以下错误。 "Failed to find handler for \"/tmp/
我正在尝试在 macOS 应用程序中将自定义类保存并检索到 UserDefaults。我的 newData 得到 nil class countClass: NSObject, NSCoding {
我有两个 ansible 任务,它们下载存档(例如最新的 wordpress 版本)并提取该存档。 - name: Download WordPress tags: - wordpress
我想从 tar 文件中提取一个目录。 在用于安装目录解包的 Linux 操作系统中 - 我只是这样做: tar -xvf ingres.tar install 对于ansible,我试过: unarc
我正在尝试执行一个 ansible 剧本,该剧本的任务是使用 unarchive 模块。因为我在 OSX 上执行此操作,所以我需要它使用 gnu-tar,而不是通常随 OSX 一起提供的 bsd ta
我一直在按照“构建你的第二个 iOS 应用程序”教程一步一步进行操作,在教程 promise 所有错误都会消失之后我遇到了这个错误,但错误直到其他错误都已更正,我尝试编译它。 错误: The docu
我写了一个 playbook 下载 tar 文件并将其解压缩到 Mac 中的某个目录。但是当我运行 playbook 时它失败并出现以下错误 "msg": "Failed to find handle
我的代码在 iOS 7 上运行: NSData *imageData = [NSKeyedArchiver archivedDataWithRootObject:self.imageView.imag
我是一名优秀的程序员,十分优秀!