In 10.10.5 using a $-prefixed archiving key does NOT work.

Does it work in 10.11?

If not, I will file a bug.

Gerriet.

This is my test code:

#import "AppDelegate.h"

static NSString *const kArchivingKey = @"$my archiving key";    //      no bug 
without leading '$'

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (strong) NSString *myName;

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{
        /*      Archives and Serializations Programming Guide →
                Encoding and Decoding Objects → Performance Considerations says:
        
                "Avoid using “$” as a prefix for your keys. 
                The keyed archiver and unarchiver use keys prefixed with “$” 
for internal values. 
                Although they test for and mangle user-defined keys that have a 
“$” prefix, this overhead makes 
                archiving slower."

                Note: this cleary implies: using “$” as a prefix for keys is 
allowed, albeit slow.
                Fact: In 10.10.5 using “$” as a prefix for keys does NOT work.
        **/
        
        self.myName = @"สิงโต"; //      use anything ≠ nil; If "สิงโต" seems 
too scary, use "pussycat" instead.
        NSData *data = [ NSKeyedArchiver archivedDataWithRootObject: self ];
        AppDelegate *selfCopy = [ NSKeyedUnarchiver  unarchiveObjectWithData: 
data ];   
        NSLog(@"%s selfCopy = %@",__FUNCTION__, selfCopy);
}



- (NSString *)description
{
        NSString *s = [ NSString stringWithFormat: @"[%@ name: %@]", [self 
class], self.myName ];
        return s;
}


- (void)encodeWithCoder:(NSCoder *)coder
{
        [ coder encodeObject: self.myName  forKey: kArchivingKey ];
}



- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
        if ( self == nil ) return nil;
        
        _myName = [ decoder decodeObjectForKey: kArchivingKey ];
        
        if ( _myName == nil )   //      error → try badKey instead
        {
                NSLog(@"%s Error: got nil for kArchivingKey = 
\"%@\"",__FUNCTION__, kArchivingKey);
                NSString *badKey = [ @"$$" stringByAppendingString: 
kArchivingKey ];
                _myName = [ decoder decodeObjectForKey: badKey ];
                
                if ( _myName == nil )   //      ok
                {
                        NSLog(@"%s Ok got nil for badKey = \"$$\" + 
kArchivingKey",__FUNCTION__);
                }
                else                                    //      error
                {
                        NSLog(@"%s Error: badKey = \"$$\" + kArchivingKey → 
%@",__FUNCTION__, _myName);
                };
        }
        else                                    //      ok
        {
                NSLog(@"%s Ok kArchivingKey = \"%@\" → %@",__FUNCTION__, 
kArchivingKey, _myName);
        };
        
        return self;
}

@end


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to