req = ["a":nil, "b":3, "c":["d":3.3, "e":"e", "f":[]], "g":[]]
Traditionally ("if js can do it we can do it too!") if you wanted to just throw any old object in storage you would ...
req: [String: Any?] ... typical api package or whatever
let enc: Data = try! NSKeyedArchiver.archivedData(
withRootObject: req, requiringSecureCoding: false)
eg, UserDefaults.standard.set(enc, forKey: "data_rqst")
and then out
let enc: Data = UserDefaults.standard.object(forKey: "data_rqst") as? Data,
let rec = NSKeyedUnarchiver.unarchiveObject(with: enc) as? [String: Any?],
Sadly these days,
'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead
Is there a replacement which allows you to [String: Any?] ?
Unfortunately solutions and mentions I've found are when you have a codable class.
I was surprised to see (unless I have the syntax wrong) you apparently cannot simply use something like [String: Any?]?
with .self
as the class in unarchivedObjectOfClass#fromData
(or "classes") calls.
[String: Any]
directly. – Subterraneanrequest
up top of the question.) You know what I believe the second approach fails anyway with nesting/optional like that @Subterranean . I was surprised to see (unless I have the syntax wrong) you can't simply use something like[String: Any?]?
with.self
as the class inunarchivedObjectOfClass#fromData
(or "classes") calls. Ah well. – Circulationlet enc = UserDefaults.standard.data(forKey: "data_rqst")!; let unarchiver = try! NSKeyedUnarchiver(forReadingFrom: enc); unarchiver.requiresSecureCoding = false; let rec = try! unarchiver.decodeTopLevelObject(of: [NSDictionary.self, NSString.self, NSNumber.self, NSArray.self, NSNull.self], forKey: NSKeyedArchiveRootObjectKey) as! [String: Any?]; print(rec);
– Supersensitivereq = ["a":nil, "b":3, "c":["d":3.3, "e":"e", "f":[]], "g":[]]
– Circulation