【问题标题】:How to fix "This coder only encodes objects that adopt NSSecureCoding (object is of class '_SwiftValue')." crash from Crashlytics如何修复“此编码器仅对采用 NSSecureCoding 的对象进行编码(对象属于 '_SwiftValue' 类)。” Crashlytics 崩溃
【发布时间】:2021-04-09 20:51:24
【问题描述】:

在上次更新我们的应用程序后,我遇到了异常崩溃的问题:

-[NSXPCEncoder _checkObject:]: This coder only encodes objects that adopt NSSecureCoding (object is of class '_SwiftValue').

我试图删除我在此更新中添加的库,但没有帮助。奇怪的是,它只在 iOS 12 上崩溃。我们从 iOS 9 开始支持。我无法重现崩溃。

Crashlytic log:

Fatal Exception: NSInvalidArgumentException
*** -[NSXPCEncoder _checkObject:]: This coder only encodes objects that adopt NSSecureCoding (object is of class '_SwiftValue').

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x22fea9ea4 __exceptionPreprocess
1  libobjc.A.dylib                0x22f079a50 objc_exception_throw
2  CoreFoundation                 0x22fdb0484 -[NSCache init]
3  Foundation                     0x2308398a0 -[NSXPCEncoder _checkObject:]
4  Foundation                     0x2308395e0 -[NSXPCEncoder _encodeUnkeyedObject:]
5  Foundation                     0x23083a27c -[NSXPCEncoder _encodeArrayOfObjects:forKey:]
6  Foundation                     0x230839b78 -[NSXPCEncoder _encodeObject:]
7  Foundation                     0x23083a27c -[NSXPCEncoder _encodeArrayOfObjects:forKey:]
8  Foundation                     0x230868ed8 -    [NSDictionary(NSDictionary) encodeWithCoder:]
9  Foundation                     0x230839b78 -[NSXPCEncoder _encodeObject:]
10 Foundation                     0x23083a27c -[NSXPCEncoder _encodeArrayOfObjects:forKey:]
11 Foundation                     0x230839b78 -[NSXPCEncoder _encodeObject:]
12 Foundation                     0x23083a27c -[NSXPCEncoder _encodeArrayOfObjects:forKey:]
13 Foundation                     0x230868ed8 -[NSDictionary(NSDictionary) encodeWithCoder:]
14 Foundation                     0x230839b78 -[NSXPCEncoder _encodeObject:]
15 Foundation                     0x23083a27c -[NSXPCEncoder _encodeArrayOfObjects:forKey:]
16 Foundation                     0x230868ed8 -[NSDictionary(NSDictionary) encodeWithCoder:]
17 Foundation                     0x230839b78 -[NSXPCEncoder _encodeObject:]
18 UserNotifications              0x23a3658cc -[UNNotificationContent encodeWithCoder:]
19 Foundation                     0x230839b78 -[NSXPCEncoder _encodeObject:]
20 UserNotifications              0x23a359578 -[UNNotificationRequest encodeWithCoder:]
21 Foundation                     0x230839b78 -[NSXPCEncoder _encodeObject:]
22 Foundation                     0x230a97ed8 _NSXPCSerializationAddInvocationWithOnlyObjectArgumentsArray
23 Foundation                     0x2308369c0 -[NSXPCEncoder _encodeInvocationObjectArgumentsOnly:count:typeString:selector:isReply:into:]
24 Foundation                     0x230836170 -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:]
25 Foundation                     0x230865f3c -[NSXPCConnection _sendSelector:withProxy:arg1:arg2:arg3:]
26 Foundation                     0x230a74ab8 _NSXPCDistantObjectSimpleMessageSend3
27 UserNotifications              0x23a36dbf8 __104-[UNUserNotificationServiceConnection addNotificationRequest:forBundleIdentifier:withCompletionHandler:]_block_invoke
28 libdispatch.dylib              0x22f8e16c8 _dispatch_call_block_and_release
29 libdispatch.dylib              0x22f8e2484 _dispatch_client_callout
30 libdispatch.dylib              0x22f889bd0 _dispatch_lane_serial_drain$VARIANT$mp
31 libdispatch.dylib              0x22f88a74c _dispatch_lane_invoke$VARIANT$mp
32 libdispatch.dylib              0x22f892eb8 _dispatch_workloop_worker_thread
33 libsystem_pthread.dylib        0x22fac50dc _pthread_wqthread
34 libsystem_pthread.dylib        0x22fac7cec start_wqthread

【问题讨论】:

  • 报错信息很清楚了吧?有一个 Swift 类不采用 NSSecureCoding 但它必须这样做。
  • @vadian 可能适合更有经验的开发人员。但对我来说不是。任何想法,它应该是哪个类?谢谢!
  • 崩溃日志中无法识别类名

标签: ios swift


【解决方案1】:

在遇到同样的问题后,我发现原因是当你尝试在userInfo 中放一些东西时,它必须符合NSSecureCoding。这在错误消息中说明,但含义是:

此字典中的键必须是属性列表类型——也就是说,它们必须是可以序列化为属性列表格式的类型。

话虽如此,值得一提的是这些类型:

数组、字典、字符串、数据、日期、数字(Int/Float)、Bool

我为自己找到的解决方案是将对象存储为Data,然后传递到userInfo

【讨论】:

  • 为什么值得一提这些类型?
  • 因为这些是唯一有效的属性列表类型。
猜你喜欢
  • 2021-05-18
  • 2021-11-26
  • 2016-07-06
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多