@eskimo: Sorry for my intervention being slightly off-topic, but I had been reading around on different forums about NSKeyedArchiving with NSCoding, and miwashi’s remark that compiling with a previous version of Swift would help, gave me some hope. However, the Swift version doesn’t seem to make a difference (apart from depreciated methods and the introduction of NSSecureCoding).
My particular problem is that my clients have a bunch of legacy files written by an Obj-C document-based app of mine, that makes use (in MyDocument)
of -dataOfType:error:
( [NSKeyedArchiver archivedDataWIthRootObject:docModel]
),
and of -readFromData:ofType:error:
( docModel = [NSKeyedUnarchiver unarchiveObjectWithData:data]
).
BTW, in it I was also using custom encoding/decoding -encodeWithCoder: / -initWithCoder
The Swift(UI) app I am developing now (and which will eventually replace the legacy Obj-C app) should continue to support the old archiving format (a binary plist) till I am ready to publish the new app. (Therefore switching from NSCoding to Codable if no option for me at this time).
Reimplementing archiving/unarchiving in Swift returned many errors, some caused by illegal format of the archived file, whatever I tried (further complicated by some depreciations since macOS 10.14). When archiving/unarchiving started to work in Swift on minimal test cases, the legacy Obj-C program also couldn’t read the files the Swift version wrote, and vice versa.
Some errors also were plainly my fault, because I didn’t use the correct type in the custom encode/coding operations.
After studying the data format for the bplist (plutil is my friend!) I found out that the class names that the Swift version uses are prefixed by the Module name (in this case the app name).
Eventually the trick seems to be to declare all the classes that need archiving as
@objc(className) class className: NSObject, NSSecureCoding
,
which effectively removes the module prefix from the className appearing in the bplist data.
Some error message hat I got in my quest, referred to the DocModel class not being found (because they were prefixed by Swift by the annoying module name). In the end, this error message (cryptic as it was), referred to the solution.
It would save a lot of developers much time if the documentation would emphasize this difference in class naming/visibility between Obj-C and Swift.
I appreciate that recent Swift-based documentation, which is being entirely overhauled, doesn’t refer to tutorials of programming guides, like in the past. I hope Apple fixes that.
I am also still looking for a tutorial explaining at least the concepts of the inner workings of keyed(un)archiving.
Thanks, eskimo, for responding to my post. I hope it is of help to some readers. I may repost the above on forums where it may be more on-topic.