Swift:nskeyedunarchiver 失败并使用"NSInvalidArgumentException"终止应用程序,原因:"-[__NSCFData计数]



NSKeyedUnarchiver。UnarchivedArrayOfObjects((使用"NSInvalidArgumentException"终止应用程序,原因:"-[__NSCFData count]

  1. 跟踪符合NSManagedObject然而,decodedTrackLocation被声明为@Published public var decodedTrackLocation: [CLLocation] = [],因为它不是持久的,而是应该在内存中。

  2. 下面的代码适用于某些"保存",但有时它会抛出异常,我不知道该怎么办

  3. 它在"iflet final="处崩溃,并抛出以下异常:

2021-06-01 17:42:28.052628+0200 GeoTrack[12300:1124356] -[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50
2021-06-01 17:42:28.061055+0200 GeoTrack[12300:1124356] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50'
*** First throw call stack:
(0x1a9e27298 0x1bdb81480 0x1a9d362a8 0x1a9e298f4 0x1a9e2b89c 0x1ad67168c 0x1ad707e24 0x1026e5988 0x102704310 0x1b0345f94 0x1b0345fb0 0x1b0345f94 0x1b0737d74 0x1b0733e68 0x1b02225f4 0x1b088b190 0x1031f7ae8 0x102ee7ae8 0x102ee932c 0x102ef776c 0x1a9da62e0 0x1a9da0740 0x1a9d9f818 0x1c04a5570 0x1ac6cb0e8 0x1ac6d0664 0x1025696dc 0x1a9a7e140)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50'
terminating with uncaught exception of type NSException
  1. NSKeyedUnarchiver失败的原因是我做了一些愚蠢的事情,我存储了错误的数据。我不明白的是,为什么这个代码所在的do try-catch块不起作用/catch。如有任何帮助,我们将不胜感激

问题中的代码:

do{
let out = try Data(referencing: NSData(data: track.trackLocation).decompressed(using: .lzma))
let archiver = try NSKeyedUnarchiver(forReadingFrom: out)
archiver.requiresSecureCoding = true
archiver.decodingFailurePolicy = .setErrorAndReturn
if let final = archiver.decodeArrayOfObjects(ofClass: CLLocation.self, forKey: NSKeyedArchiveRootObjectKey){
track.decodedTrackLocation = final
}
} catch {
print(error)
}
do{
let out = try Data(referencing: NSData(data: track.trackLocation).decompressed(using: .lzma))
if let final = try NSKeyedUnarchiver.unarchivedArrayOfObjects(ofClass: CLLocation.self, from: out){
track.decodedTrackLocation = final
}
} catch {
print(error)
}

因为抛出的不是Error,所以它是Objective-C上的NSInvalidArgumentException,NSEException。你可以找它:(-Larme

我学到了一些新东西!

相关内容

最新更新