I have an object of class SGBContainer which has an array named objects that contains objects of class SGBObject. Currently, they each implement NSCoding but not NSSecureCoding. The -initWithCoder: for SGBContainer looks like this:
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [self init];
if (self)
{
_objects = [aCoder decodeObjectForKey:@"objects"];
}
}
I want to switch to using NSSecureCoding, and from what I can tell, it would mean changing the above to this:
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [self init];
if (self)
{
_objects = [aCoder decodeObjectOfClass:[NSArray class] forKey:@"objects"];
}
}
...which isn't much of an improvement, as the contents of the array will be instantiated whatever their class. How do I make sure that the array only contains objects of class SGBObject without instantiating them?
Although it's not at all clear from the documentation (and just sounds like an oddly ungrammatical method name), this is what -decodeObjectOfClasses:forKey: does. You'd do something like the following:
NSSet *classes = [NSSet setWithObjects:[NSArray class], [SGBObject class], nil];
_objects = [aCoder decodeObjectOfClasses:classes forKey:@"objects"];
(Credit where it's due: See NSSecureCoding trouble with collections of custom class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With