Can I ignore it?
For the moment: Yes. Ultimately: No. The warning says “This will be disallowed in the future.” and it would be unwise to ignore that.
Elements are NSString
, NSArray
, NSMutableArray
, NSNumber
,
NSSet
, NSMutableSet
All of those support secure coding out of the box, so you just need to declare which types you expect to find in your graph. Do that by calling +unarchivedObjectOfClasses:fromData:error:
.
For example, this method works but logs a bunch of warnings:
- (void)testNG {
NSLog(@"-testNG");
NSArray * aIn = @[ @1, @"one", @[ @2, @"two" ] ];
NSData * d = [NSKeyedArchiver archivedDataWithRootObject:aIn requiringSecureCoding:YES error:nil];
NSArray * aOut = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class] fromData:d error:nil];
NSLog(@"%@", aOut);
}
OTOH, this method doesn’t:
- (void)testOK {
NSLog(@"-testOK");
NSArray * aIn = @[ @1, @"one", @[ @2, @"two" ] ];
NSData * d = [NSKeyedArchiver archivedDataWithRootObject:aIn requiringSecureCoding:YES error:nil];
NSSet * allowed = [NSSet setWithArray:@[
[NSArray class], [NSString class], [NSNumber class]
]];
NSArray * aOut = [NSKeyedUnarchiver unarchivedObjectOfClasses:allowed fromData:d error:nil];
if ( ![aOut isKindOfClass:[NSArray class]]) {
… handle this failure …
}
NSLog(@"%@", aOut);
}
Keep in mind that the top-level object can be any of types listed in the allowed
parameter, so you have to check the type of the returned object.
I don't understand what you mean by "I think you should take
this opportunity to factor out the common code so that you
only have one copy of your archiving code." What do you mean
by "common code"?
IMO you shouldn’t have lots of different routines that call NSKeyed{Un,}Archiver
. Rather, change your “500 methods” to use an abstraction layer and have the routines in that abstraction layer be the only code that uses keyed archiving directly. That way fixing your 500 methods is easy — you remove a bunch of keyed archiving code and replace it with a call to your abstraction layer — and then all this weird keyed archiving stuff is done in one place.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"