• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSKeyedArchiver/NSKeyedUnarchiver
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSKeyedArchiver/NSKeyedUnarchiver


  • Subject: NSKeyedArchiver/NSKeyedUnarchiver
  • From: Michael Monscheuer <email@hidden>
  • Date: Sat, 30 Aug 2003 16:16:01 +0200

Hi!

Saving a keyed archive and analysing the resulting file, I ran into a problem of understanding:

After reading the documentation carefully, I expected a keyed saving of instance variables as I can access them via keys within initWithCoder: while decoding file. It is not:
The resulting file contains all data - but unkeyed and in the order the instance variables where encoded in encodeWithCoder:.

Although the keys are not stored to the XML file, the access by keys in initWithCoder: does work magically correct.

These facts (you may want to check this out using the little test programm I attached) made me want to ask the following questions:

1) How does NSUnarchiver find out which value matches which key, so that decodeObjectForKey: gives the correct result?
2) The written archive is readable XML, though documentation says "The format of the archive is NSPropertyListBinaryFormat_v1_0." for archiveRootObject:toFile:. The result seems not very Binary. Is this a bug? I did not managed (even using NSKeyedArchiver's setOutputFormat: method) to write another format as the XML you find attached.
3) As there are no keys for instance variables written to the file, other applications (maybe on other platforms) must rely on the application writing the varibles in a specific order and so these other applications do not profit from keyed accessability (e.g. using XPath). Is this correct?
4) Is there a way to write the instance variable keys to the file without subclassing, which I am too blind to see?

Thanks for reading,

MiMo

//
// main.m
// KeyedArchiverTest
//
// Created by Michael Monscheuer on Sat Aug 30 2003.
// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

/*******************************/
/* class interface */
/*******************************/

@interface MyObject : NSObject <NSCoding>
{
NSString *firstString;
NSString *secondString;
}

-(id)init;
-(void)setString:(NSString *)string1 andString:(NSString *)string2;
-(void)logStrings;

@end

/*******************************/
/* class implementation */
/*******************************/

@implementation MyObject

/* ------ init ------- */
-(id)init
{
self = [super init];

firstString = secondString = nil;

return self;
} // -init

/* ------ setString:andString ------- */
-(void)setString:(NSString *)string1 andString:(NSString *)string2
{
[string1 retain];
firstString = string1;

[string2 retain];
secondString = string2;
} // -setString:andString:

/* ------ logStrings ------- */
-(void)logStrings
{
NSLog(@"1:%@,2:%@\n",firstString,secondString);
} // -logStrings

/* ------ encodeWithCoder: ------- */
- (void)encodeWithCoder:(NSCoder*)coder
{
if ([coder allowsKeyedCoding])
{
[coder encodeObject:firstString forKey:@"WhereAmI_1"];
[coder encodeObject:secondString forKey:@"WhereAmI_2"];
}
else NSLog(@"not supported yet..");
} // -encodeWithCoder:

/* ------ initWithCoder: ------- */
- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super init])
{
if ([coder allowsKeyedCoding])
{
secondString = [[coder decodeObjectForKey:@"WhereAmI_2"] retain];
firstString = [[coder decodeObjectForKey:@"WhereAmI_1"] retain];
}
else NSLog(@"not supported yet..");
}

return self;
} // initWithCoder:
@end

/*******************************/
/* main */
/*******************************/

int main(int argc, const char *argv[])
{
id obid; // object id
// create autorelease pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

// create instance of MyObject
obid = [[[MyObject alloc]init]autorelease];

// set two strings to instance
[obid setString:@"HelloWorld!" andString:@"WhereAreTheKeys?"];

// log strings
NSLog(@"before:");
[obid logStrings];

// call keyed archiver
if (![NSKeyedArchiver archiveRootObject:obid toFile:@"keyedTest.xml"])
{
NSLog(@"Could not create keyed archive.");
return 1;
}
// unarchive stored file
obid = [NSKeyedUnarchiver unarchiveObjectWithFile:@"keyedTest.xml"];

// log strings of unarchived object
NSLog(@"after:");
[obid logStrings];

// release pool
[pool release];

return 0; // exit
} // main()

/* -------------------*/
/* The resulting file */
/* ------------------ */
/*
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd";>
<plist version="1.0">
<dict>
<key>$archiver</key>
<string>NSKeyedArchiver</string>
<key>$objects</key>
<array>
<string>$null</string>
<dict/>
<string>HelloWorld!</string>
<string>WhereAreTheKeys?</string>
<dict>
<key>$classes</key>
<array>
<string>MyObject</string>
<string>NSObject</string>
</array>
<key>$classname</key>
<string>MyObject</string>
</dict>
</array>
<key>$top</key>
<dict/>
<key>$version</key>
<integer>100000</integer>
</dict>
</plist>
*/

--
=======================================================
And refashioning the fashioned
lest it stiffen into iron
is work of endless vital activity.
[Goethe]

mailto:email@hidden
mailto:email@hidden

All about CALAMUS DTP suite:
http://www.calamus.net

=======================================================
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Fullscreen and receiving events
  • Next by Date: How to: Save an Array into a File at a certain path?
  • Previous by thread: Re: Fullscreen and receiving events
  • Next by thread: How to: Save an Array into a File at a certain path?
  • Index(es):
    • Date
    • Thread