Is it possible to decode a NSKeyedArchived collection containing unknown objects

157 Views Asked by At

I have a collection of objects with different classes A, B, and C (all share a common superclass):

// Encoded collection
@[A, B, C]

This collection gets archived and unarchived via NSCoding. Is there a way to partially unarchive this collection if one of the classes has since been removed from the project?

It seems decodeObjectForKey: fails and returns a nil object when it encounters an object it can't decode. Is there a way to instead only get what is able to be decoded? I understand why this behavior makes sense for an object but not for a collection.

// Expected decoding when A class definition is missing:
@[B, C]

// Actual:
nil
1

There are 1 best solutions below

5
On

If the class name and the class that you want to use instead are known then you can use the setClass:forClassName: method of NSKeyedUnArchiver to specify a translation, so you could use something like [unarchiver setClass:MySuperClass.class forClassName:@"C"]