iOS 12.0 替代使用已弃用 archiveRootObject:toFile:

iOS 12.0 Alternative to Using Deprecated archiveRootObject:toFile:

在 iOS 12 中,archiveRootObject:toFile: 已被弃用。谁能建议一种简化的替代方法来将对象归档到文件中?

//Generic example of archiver prior to iOS 12.0    
-(BOOL) archive:(id)archiveObject withFileName:(NSString*)filename
{
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    return [NSKeyedArchiver archiveRootObject:archiveObject toFile:path];
}

替换为archivedDataWithRootObject:requiringSecureCoding:error:

+ (NSData *)archivedDataWithRootObject:(id)object 
             requiringSecureCoding:(BOOL)requiresSecureCoding 
                             error:(NSError * _Nullable *)error;

加上将数据写入磁盘的额外步骤。

请看Foundation iOS 11.4 to 12.0 API Differences

感谢@vadian 的提示,这是我在 iOS 12:

下进行归档和取消归档的方法
NSError *error = nil;

NSString *docsDir;
NSArray *dirPaths;

//Get the device's data directory:
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"appData.data"]];

//Archive using iOS 12 compliant coding:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:@"foo" requiringSecureCoding:NO error:&error];
[data writeToFile:databasePath options:NSDataWritingAtomic error:&error];
NSLog(@"Write returned error: %@", [error localizedDescription]);

//Unarchive the data:
NSData *newData = [NSData dataWithContentsOfFile:databasePath];
NSString *fooString = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSString class] fromData:newData error:&error];

unArchivedObjectOfClass 在尝试解码未使用安全编码的对象时向我抛出错误。经过多次试验和错误,这终于在没有触发 iOS 12/13 弃用警告的情况下起作用:

// Archive the object
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:theObject requiringSecureCoding:NO error:nil];

// Unarchive the object
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:nil];
unarchiver.requiresSecureCoding = NO;
id theCopy = [unarchiver decodeTopLevelObjectForKey:NSKeyedArchiveRootObjectKey error:nil];

作为suggested by Apple,我们应该使用FileManager来read/write归档文件。

func archiveURL() -> URL? {
    guard let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first 
        else { return nil }

    return documentURL.appendingPathComponent("MyArchive.data")
}

func archive(customObject: CustomObject) {
    guard let dataToBeArchived = try? NSKeyedArchiver.archivedData(withRootObject: customObject, requiringSecureCoding: true), 
        let archiveURL = archiveURL() 
        else  {
        return
    }

    try? dataToBeArchived.write(to: archiveURL)
}

func unarchive() -> CustomObject? {
    guard let archiveURL = archiveURL(),
        let archivedData = try? Data(contentsOf: archiveURL),
        let customObject = (try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archivedData)) as? CustomObject 
        else {
        return nil
    }

    return customObject
}