iOS归档对象适用于设备,但不适用于模拟器

时间:2014-08-19 23:12:45

标签: ios simulator nskeyedarchiver

我正在使用NSKeyedArchiver的常用代码,保存并加载单个NSMutableArray。在我的iPhone和iPad上,我得到完整的阵列;在模拟器上,我得到一个空数组。不是nil,而是一个空的有效数组。在应用程序的其他地方,其他归档在模拟器和设备上运行良好。

我在代码中包含了一个潜伏的错误,但我很困惑。

- (NSString*) personImageArchivePath {
    NSArray* docFolderPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                              NSUserDomainMask, YES);
    NSString* docFolderPath = docFolderPaths[0];
    return [docFolderPath stringByAppendingPathComponent:@"personImage.archive"];
}

- (void) savePersonImages {
    NSLog(@"Archiving %ld person images", (unsigned long)self.personImages.count);
    BOOL success = [NSKeyedArchiver archiveRootObject:self.personImages 
                                               toFile:[self personImageArchivePath]];
    NSString* result = success ? @"succeeded" : @"failed";
    NSLog(@"saving person images %@", result);

    // Sanity check
    NSMutableArray* reloadedArray = [NSKeyedUnarchiver 
        unarchiveObjectWithFile:[self personImageArchivePath]];
    if (reloadedArray == nil) {
        NSLog(@"Sanity check: No person images found");
    } else {
        NSLog(@"Sanity check: Unarchived %ld person images", 
                (unsigned long)reloadedArray.count);
    }
}

- (NSMutableArray*) loadPersonImages
{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[self personImageArchivePath]];
    // This returns nil (of course) on first launch, all platforms.
    // On the devices subsequent calls return the full array that was archived;
    // on the simulator they return an empty array.
}

0 个答案:

没有答案