NSKeyedUnarchiver.UnarchivedArrayOfObjects() terminates app with 'NSInvalidArgumentException', reason: '-[__NSCFData count]

  1. track conforms to NSManagedObject However decodedTrackLocation is declared @Published public var decodedTrackLocation: [CLLocation] = [], as it is not persistent, but supposed to be in memory.

  2. The below code works on certain 'saves', but sometimes it throws an exception and I can't figure out what to do about it.

  3. It crashes at 'if let final =' and throws this exception:

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. The reason the NSKeyedUnarchiver fails is because I did something stupid, I stored the wrong Data. What I can't grasp is why the do-try-catch block this code lives in, doesn't work/catch. Any help would be appreciated.

The code in Question:

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)
}
1

There are 1 answers

0
Matter4478 On

Because it's not an Error that is thrown (throws), it's a NSInvalidArgumentException, a NSException, on Objective-C one. You can look for it :) - Larme

I learned something new!