Re: Verbose warning splurged to the log with Secure Coding (dearchiving) -- can they be serious?
Graham Cox
Well, I figured out a solution, though I’m not sure — it seems slightly smelly, even though it looks neat from the outside and is easy to use. Would appreciate any feedback.
toggle quoted message
Show quoted text
Basically, I walk up the class hierarchy until I hit NSObject, then return the class once removed from that. Then I added a category on NSCoder that uses this to encode that class name in the archive. Using these methods in NSCoding is easy and hides the minor pain involved. Class GCLowestSpecificAncestorClass( Class aClass ) { // walks back up the class hierarchy from <aClass> until it reaches NSObject (or nil), and returns the // class that subclasses NSObject. Class sc = [aClass superclass]; while( sc && sc != [NSObject class]) { aClass = sc; sc = [aClass superclass]; } return aClass; } @implementation NSCoder (GCSecureAnonymousClassSupport) - (void) encodeAnonymousObject:(id) theObject forKey:(NSString*) key { Class umbrella = GCLowestSpecificAncestorClass([theObject class]); NSAssert( umbrella != [NSObject class], @"cannot archive NSObject as anonymous class"); NSString* className = NSStringFromClass( umbrella ); [self encodeObject:className forKey:[NSString stringWithFormat:@"class_for_%@", key]]; [self encodeObject:theObject forKey:key]; } - (id) decodeAnonymousObjectForKey:(NSString*) key { NSString* className = [self decodeObjectOfClass:[NSString class] forKey:[NSString stringWithFormat:@"class_for_%@", key]]; Class umbrella = NSClassFromString( className ); return [self decodeObjectOfClass:umbrella forKey:key]; } @end
|
|