1

I'm hitting the issue documented here and I'm trying to utilize NSKeyedUnarchiver.unarchivedDictionary, but I'm not sure what KeysofClasses or objectsOfClasses is wanting.

The structure of my dictionary is: [Int: [String: String]]

A bit of my code:

let encodedData = try NSKeyedArchiver.archivedData(withRootObject: widgetDictionary, requiringSecureCoding: false)

let outData = UserDefaults.standard.data(forKey: "flaggedWidgetDictKey")
let decodedData = NSKeyedUnarchiver.unarchivedDictionary(keysOfClasses:  [Int: [String: String]], objectsOfClasses: [Int: [String: String]], from: outData!))

The error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object for key jobCategory'

Robert
  • 809
  • 3
  • 10
  • 19
  • Seeing the error, it seems that `archivedData` is creating the issue... What is `widgetDictionary`? – Larme Jun 02 '22 at 16:46
  • the `archivedData` is my attempt to resolve the crash. Before that I was doing `userDefaults?.set(widgetDictionary, forKey: "flaggedWidgetDictKey")` The `widgetDictionary` is `[0: ["Foo" : "Data"]]` – Robert Jun 02 '22 at 16:54
  • 1
    Concerning the previous error, I'm wondering if that's because the key of the diictionary can't be `Int` to be PropertyList Compliant, (as it's the same for JSON), to verify... – Larme Jun 02 '22 at 16:59
  • That would be interesting if `Int` could not be a key since Apple's documentation says it can https://developer.apple.com/documentation/swift/dictionary or maybe I'm reading it wrong. – Robert Jun 02 '22 at 17:02
  • 1
    I meant in a "plist". For instance, you can put a lot of things as key in a dictionary, but in JSON, only strings keys are allowed. So it could have been the same for Plist compliance. But it seems it doesn't matteer. Now, I'd avoiid `NSArchiver`, especially for just a Dictionary. I'd use Plist Encoder (since it's not JSON compatible). – Larme Jun 07 '22 at 10:01

0 Answers0