swift - 如何在 Swift 中捕获 NSKeyedUnarchiver“NSInvalidUnarchiveOperationException”?
问题描述
我正在使用Codable
withNSKeyedArchiver
和NSKeyedUnarchiver
Swift 来处理/解码对象。
当编码的对象与预期的格式匹配时,事情就会很好。但是,如果数据无效或格式不匹配,我会在单元测试中得到以下断言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
)
解决方案
从 iOS 9 开始,您可以使用: unarchiveTopLevelObjectWithData(_:)
,它会引发异常。
例子:
try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)
推荐阅读
- awk - 根据bash中的两列计算唯一值的数量
- wordpress - WordPress REST API 端点 - 如何在同一端点上允许多个可选 ID?
- python - 连接线图 seaborn 中的标记
- elasticsearch - Elastic Search 可以用作 NoSQL 数据库吗
- excel - VBA Excel ListView 复选框不显示在用户窗体中
- python - Pandas:转置 df 并使用列名创建多索引
- java - 如何让我的 jTextFields 一个接一个地显示
- google-bigquery - 如何在不逐行运行每组条件的每个更新语句的情况下批量更新
- r - 无法在 r 中安装 EBImage 包
- python-3.x - Pandas:过滤掉包含两个期望值的行(唯一 ID)