I looked through whole SO but still no answer. My app reports this problem:
Fatal Exception: NSInvalidUnarchiveOperationException *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (App_Title.Products) for key (NS.objects); the class may be defined in source code or a library that is not linked
I did already do NSKeyedUnarchiver.setClass just before #unarchiveObject:
func loadProducts() -> [Products]? {
NSKeyedUnarchiver.setClass(Products.self, forClassName: "Products")
let unarchivedData = NSKeyedUnarchiver.unarchiveObject(withFile: Products.ArchiveURL.path)
My Products class begins with @Objc:
import Foundation
@objc(Products)
class Products: NSObject, Codable, NSCoding { ... }
Adding the two lines above which seemed to help people didn't bring me any luck. Before them and after them it's the same behaviour. I personally could never reproduce this issue myself.
During development I keep very closely to the app guide on peristance and reviewed it multiple times.
Just before NSKeyedArchiver I check for file existance:
let filePath = Products.ArchiveURL.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
Here some additional informations in screenshots.
The only place where I could find a real exception description was Firebase Crashalytics:
A screenshot from the Xcode Organizer under the Crash tab:
Which leads to this line in the code:
NSKeyedUnarchiver.setClass(Products.self, forClassName: "Products")
let unarchivedData = NSKeyedUnarchiver.unarchiveObject(withFile: Products.ArchiveURL.path)
Products class with @Objc annotation.