swift - 如何在 Swift 中捕获 NSKeyedUnarchiver "NSInvalidUnarchiveOperationException"?

标签 swift codable nskeyedunarchiver

我在 Swift 中使用 CodableNSKeyedArchiverNSKeyedUnarchiver 来编码/解码对象。

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

failed: caught "NSInvalidUnarchiveOperationException", "The data couldn’t be read because it isn’t in the correct format."

我正在使用 NSKeyedUnarchiver.decodeTopLevelDecodable 方法,which is supposed to throw an exception解码失败时:

If the archive is not a valid property list, this method throws the DecodingError.dataCorrupted(_:) error. If a value within the archive fails to decode, this method throws the corresponding error.

我无法捕获此异常,因为 try?do/catchXCTAssertThrowsError 似乎都不起作用。

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

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
)

最佳答案

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

例子:

try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)

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

关于swift - 如何在 Swift 中捕获 NSKeyedUnarchiver "NSInvalidUnarchiveOperationException"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51801808/

相关文章:

ios - 具有原始值的枚举,可编码

ios - NSKeyedArchiver 不持久保存数据

json - 为什么我的 json 不会将列表中的所有对象解析为带有标题的不同行?

ios - objectForKey 在 Swift 中为 NSUserDefaults 返回 nil

ios - 在 SwiftUI 中使用像 MVC 这样的设计模式

ios - Swift `Decodable` 不适用于嵌套的 JSON 对象,除非嵌套是一个数组

ios - 解析查询结果未添加(附加)到 iOS Swift 中的本地数组

json - 如何从 Swift Codable 中排除属性?

arrays - iOS12 中的 NSKeyedUnarchiver 更改 - 如何取消存档字符串数组

ios - 无法解码 Swift 中保存的信息