0

i am on objective-c, macOS.

I am currently rewriting an application to support NSSecureCoding Protocol. In some classes i have some properties like this

@property id objectValue;

These objects can be of very dynamic kind (NSString, custom object ...) On loading i did this before:

_objectValue = [coder decodeObjectForKey:@"objectValue"];

With NSSecureCoding i need to specify the class which i cannot do with id obviously. So here are my questions:

  1. Is it OK to pass a superclass like NSObject even if it's basically an NSString?

    _objectValue = [coder decodeObjectOfClass:[NSObject class] forKey:@"objectValue"];
    

    But... doesn't this counteract the idea of NSSecureCoding?

  2. Is there another (better) approach?

Edit: Just seen that this is possible with multiple classes decodeObjectOfClasses:forKey: - would the correct approach be to list all classes possible?

Pat_Morita
  • 3,355
  • 3
  • 25
  • 36
  • Per your edit, the answer is yes - you just list all potential classes that could be unarchived from the archive. – Rudedog Jan 07 '21 at 17:12
  • Is there another approach? Well, not ideal, but if you can wrap all your classes into a property list and use ```NSPropertyListSerialization``` you sidestep a lot of the issues and later when your classes change it might be easier to handle. You can e.g. add a ```version``` when you serialise to a property list and then it is really easy later on when you deserialise to support older versions. – skaak Jan 08 '21 at 10:44

0 Answers0