The Beruriah Incident
The Beruriah Incident

Reputation: 3257

How to decode dictionary of dictionaries with NSSecureCoding

I have an existing iOS app with years of serialization using NSCoding. As NSCoding is deprecated in favor of NSSecureCoding, I'm switching over. Most data has been very straightforward, but I can't figure how to decode dictionaries of dictionaries.

For example, with classic NSCoding:

if let aToBToC = decoder.decodeObject(forKey: aToBToCKey) as? [String: [String: String]] {
    self.aToBToC = aToBToC
}

When I try to write this with NSSecureCoding, I can't figure what to use as the object type:

if let aToBToC = decoder.decodeObject(withKeyClass: NSString.self, objectClass: ?????, forKey: aToBToCKey) as? [String: [String: String]] {
    self.aToBToC = aToBToC
}

Is there perhaps someway to make an AnyClass for [String: String]?

As an analogy, the way this is solved in Java with Jackson deserialization is new TypeReference<Map<String, String>>(): i.e. there's a break glass way to define more complicated types than String.class. Does something similar exist for Swift?

I have the constraint that I can't change the serialization format without breaking all existing users.

Upvotes: 0

Views: 100

Answers (0)

Related Questions