如何在 Mojave 中存档 NSSortDescriptor 数组
How to archive an array of NSSortDescriptor in Mojave
我有通过 High Sierra (10.13) 运行的代码,它成功地将一组排序描述符归档和取消归档到 NSData,但操作调用现在已在 Mojave (10.14) 中弃用。这是有效的代码:
NSArray<NSSortDescriptor*> *sortSettings = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:NO]];
NSData *sortData = [NSKeyedArchiver archivedDataWithRootObject:sortSettings];
NSArray<NSSortDescriptor*> *unarchivedSettings = [NSKeyedUnarchiver unarchiveObjectWithData:sortData];
// Test case passes
XCTAssertEqualObjects(unarchivedSettings.firstObject.key, @"title");
我已经尝试使用下面的代码,以及弃用警告所建议的调用,但它不起作用。
NSArray<NSSortDescriptor*> *sortSettings = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:NO]];
NSError *archiveError = nil;
NSData *sortData = [NSKeyedArchiver archivedDataWithRootObject:sortSettings
requiringSecureCoding:YES
error:&archiveError];
// Test cases pass
XCTAssertNotNil(sortData);
XCTAssertNil(archiveError);
NSError *unarchiveError = nil;
NSArray<NSSortDescriptor*> *unarchivedSettings = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class]
fromData:sortData
error:&unarchiveError];
XCTAssertNotNil(unarchivedSettings); // Fails
XCTAssertNil(unarchiveError); // Fails, with error below
归档成功,返回一个NSData,但是unarchive调用returns报错:
Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'NS.objects' was of unexpected class 'NSSortDescriptor'. Allowed classes are '{(
NSArray
)}'." UserInfo={NSDebugDescription=value for key 'NS.objects' was of unexpected class 'NSSortDescriptor'. Allowed classes are '{(
NSArray
)}'.}
您需要列出所有正在存档的 类(在本例中为 NSArray
和 NSSortDescriptor
):
[NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithArray:@[
[NSArray class],
[NSSortDescriptor class]
]]
fromData:sortData
error:&unarchiveError];
我有通过 High Sierra (10.13) 运行的代码,它成功地将一组排序描述符归档和取消归档到 NSData,但操作调用现在已在 Mojave (10.14) 中弃用。这是有效的代码:
NSArray<NSSortDescriptor*> *sortSettings = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:NO]];
NSData *sortData = [NSKeyedArchiver archivedDataWithRootObject:sortSettings];
NSArray<NSSortDescriptor*> *unarchivedSettings = [NSKeyedUnarchiver unarchiveObjectWithData:sortData];
// Test case passes
XCTAssertEqualObjects(unarchivedSettings.firstObject.key, @"title");
我已经尝试使用下面的代码,以及弃用警告所建议的调用,但它不起作用。
NSArray<NSSortDescriptor*> *sortSettings = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:NO]];
NSError *archiveError = nil;
NSData *sortData = [NSKeyedArchiver archivedDataWithRootObject:sortSettings
requiringSecureCoding:YES
error:&archiveError];
// Test cases pass
XCTAssertNotNil(sortData);
XCTAssertNil(archiveError);
NSError *unarchiveError = nil;
NSArray<NSSortDescriptor*> *unarchivedSettings = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class]
fromData:sortData
error:&unarchiveError];
XCTAssertNotNil(unarchivedSettings); // Fails
XCTAssertNil(unarchiveError); // Fails, with error below
归档成功,返回一个NSData,但是unarchive调用returns报错:
Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'NS.objects' was of unexpected class 'NSSortDescriptor'. Allowed classes are '{( NSArray )}'." UserInfo={NSDebugDescription=value for key 'NS.objects' was of unexpected class 'NSSortDescriptor'. Allowed classes are '{( NSArray )}'.}
您需要列出所有正在存档的 类(在本例中为 NSArray
和 NSSortDescriptor
):
[NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithArray:@[
[NSArray class],
[NSSortDescriptor class]
]]
fromData:sortData
error:&unarchiveError];