How to deal with module name in unarchiving?

I used NSKeyedArchiver.archiveRootObject(_,toFile:) in a command-line app to archive an instance of a Swift class. I am unable to unarchive in another, interactive, Swift app, getting the error message:


-[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (AnAppName.AClassName) for key (root); the class may be defined in source code or a library that is not linked


The compiler has the AClassName.swift file and recognizes it OK. I presume the problem is that the archiver prepended the app name (as module?) and the unarchiver doesn't know what to do with it. The file is pretty expensive to produce, so I'd rather not redo it, if that can be avoided.


I guess that I should have created a Framework that could be linked to both the batch and interactive apps, but who knew?


Any ideas?


Richard Nerf

Accepted Reply

I haven't run into this (yet!) but I suggest you try using the "setClass(_:forClassName:)" instance method of NSKeyedUnarchiver, specifying "AnAppName.AClassName" for the class name string, and AClassName.self as the replacement class.

Replies

I haven't run into this (yet!) but I suggest you try using the "setClass(_:forClassName:)" instance method of NSKeyedUnarchiver, specifying "AnAppName.AClassName" for the class name string, and AClassName.self as the replacement class.

Quincey,


Your suggestion did fix the error message I encountered. Thanks!


Richard