首页 > 解决方案 > 如何在 Swift 中捕获 NSKeyedUnarchiver“NSInvalidUnarchiveOperationException”?

问题描述

我正在使用CodablewithNSKeyedArchiverNSKeyedUnarchiverSwift 来处理/解码对象。

当编码的对象与预期的格式匹配时,事情就会很好。但是,如果数据无效或格式不匹配,我会在单元测试中得到以下断言XCTest

失败:捕获“NSInvalidUnarchiveOperationException”,“无法读取数据,因为它的格式不正确。”

我正在使用NSKeyedUnarchiver.decodeTopLevelDecodable方法,它应该在解码失败时抛出异常:

如果存档不是有效的属性列表,则此方法会引发DecodingError.dataCorrupted(_:)错误。如果存档中的某个值无法解码,则此方法会引发相应的错误。

我无法捕捉到这个异常,因为既不工作try?do/catch也不XCTAssertThrowsError工作。

测试失败时的完整错误消息:

failed: caught "NSInvalidUnarchiveOperationException", "The data couldn’t be read because it isn’t in the correct format."
(
    0   CoreFoundation                      0x00007fff315f12db __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff587a1c76 objc_exception_throw + 48
    2   Foundation                          0x00007fff3376b077 -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 0
    3   Foundation                          0x00007fff3376b371 -[NSCoder(Exceptions) __failWithExternalError:] + 161
    4   libswiftFoundation.dylib            0x0000000109872453 _T0So17NSKeyedUnarchiverC10FoundationE23decodeTopLevelDecodablexSgxm_SS6forKeytKs0G0RzlF + 403
    5   MBOUtilityKit                       0x0000000104cba964 _T013MBOUtilityKit17KeyedLocalStorageV3getxSS3key_tKs9DecodableRzs9EncodableRzlF + 772
    6   MBOUtilityKitTests macOS            0x0000000104c5144c _T024MBOUtilityKitTests_macOS017KeyedLocalStorageC0C17testDecodeFailureyyKF + 556
    7   MBOUtilityKitTests macOS            0x0000000104c51835 _T024MBOUtilityKitTests_macOS017KeyedLocalStorageC0C17testDecodeFailureyyKFTo + 69
    8   CoreFoundation                      0x00007fff31568bec __invoking___ + 140
    9   CoreFoundation                      0x00007fff31568ac0 -[NSInvocation invoke] + 320
    10  XCTest                              0x000000010036c90d __24-[XCTestCase invokeTest]_block_invoke_2.187 + 65
    11  XCTest                              0x00000001003d6207 -[XCTMemoryChecker _assertInvalidObjectsDeallocatedAfterScope:] + 51
    12  XCTest                              0x00000001003755ef -[XCTestCase assertInvalidObjectsDeallocatedAfterScope:] + 116
    13  XCTest                              0x000000010036c89c __24-[XCTestCase invokeTest]_block_invoke.181 + 210
    14  XCTest                              0x00000001003c8772 +[XCTestCase(Failures) performFailableBlock:shouldInterruptTest:] + 36
    15  XCTest                              0x00000001003c86bc -[XCTestCase(Failures) _performTurningExceptionsIntoFailuresInterruptAfterHandling:block:] + 54
    16  XCTest                              0x000000010036c4db __24-[XCTestCase invokeTest]_block_invoke + 854
    17  XCTest                              0x00000001003cd659 -[XCUITestContext performInScope:] + 237
    18  XCTest                              0x000000010036c170 -[XCTestCase invokeTest] + 175
    19  XCTest                              0x000000010036dea6 __26-[XCTestCase performTest:]_block_invoke_2 + 42
    20  XCTest                              0x00000001003c8772 +[XCTestCase(Failures) performFailableBlock:shouldInterruptTest:] + 36
    21  XCTest                              0x00000001003c86bc -[XCTestCase(Failures) _performTurningExceptionsIntoFailuresInterruptAfterHandling:block:] + 54
    22  XCTest                              0x000000010036dd53 __26-[XCTestCase performTest:]_block_invoke.326 + 90
    23  XCTest                              0x00000001003d24a1 +[XCTContext runInContextForTestCase:block:] + 225
    24  XCTest                              0x000000010036d454 -[XCTestCase performTest:] + 673
    25  XCTest                              0x00000001003b0555 -[XCTest runTest] + 57
    26  XCTest                              0x0000000100368b30 __27-[XCTestSuite performTest:]_block_invoke + 365
    27  XCTest                              0x000000010036830c -[XCTestSuite _performProtectedSectionForTest:testSection:] + 55
    28  XCTest                              0x00000001003685cd -[XCTestSuite performTest:] + 296
    29  XCTest                              0x00000001003b0555 -[XCTest runTest] + 57
    30  XCTest                              0x0000000100368b30 __27-[XCTestSuite performTest:]_block_invoke + 365
    31  XCTest                              0x000000010036830c -[XCTestSuite _performProtectedSectionForTest:testSection:] + 55
    32  XCTest                              0x00000001003685cd -[XCTestSuite performTest:] + 296
    33  XCTest                              0x00000001003b0555 -[XCTest runTest] + 57
    34  XCTest                              0x0000000100368b30 __27-[XCTestSuite performTest:]_block_invoke + 365
    35  XCTest                              0x000000010036830c -[XCTestSuite _performProtectedSectionForTest:testSection:] + 55
    36  XCTest                              0x00000001003685cd -[XCTestSuite performTest:] + 296
    37  XCTest                              0x00000001003b0555 -[XCTest runTest] + 57
    38  XCTest                              0x00000001003e6308 __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke + 40
    39  XCTest                              0x000000010038aaa4 -[XCTestObservationCenter _observeTestExecutionForBlock:] + 600
    40  XCTest                              0x00000001003e610e -[XCTTestRunSession runTestsAndReturnError:] + 369
    41  XCTest                              0x000000010034f865 -[XCTestDriver runTestsAndReturnError:] + 440
    42  XCTest                              0x00000001003d10f3 _XCTestMain + 1228
    43  xctest                              0x0000000100002155 main + 557
    44  libdyld.dylib                       0x00007fff593bb015 start + 1
    45  ???                                 0x0000000000000005 0x0 + 5
)

标签: swiftcodablenskeyedunarchiver

解决方案


从 iOS 9 开始,您可以使用: unarchiveTopLevelObjectWithData(_:),它会引发异常。

例子:

try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)

https://developer.apple.com/documentation/foundation/nskeyedunarchiver/2919664-unarchivetoplevelobjectwithdata


推荐阅读