- (NSData *) dataOfType: (NSString *) typeName
error: (NSError **) error
{
*error = nil;
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver;
archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData: data];
[archiver setOutputFormat: NSPropertyListXMLFormat_v1_0];
[archiver encodeObject: stitches forKey: @"stitches"];
// and archive other stuff you want
[archiver finishEncoding];
[archiver release];
return ([data autorelease]);
} // dataOfType
- (BOOL) readFromData: (NSData *) data
ofType: (NSString *) typeName
error: (NSError **) error
{
*error = nil;
NSKeyedUnarchiver *archiver;
archiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData: data];
[stitches release];
stitches = [archiver decodeObjectForKey: @"stitches"];
// decode other stuff of interest
[stitches retain];
return (YES);
} // readFromData
StitchEdit[3522] *** class error for 'BWRawPath': class not loaded
In a convenient place (like in the +load method of your class), use NSUnarchiver's decodeClassName:asClassName:
@implementation BWSymbol
+ (void) load
{
[NSUnarchiver decodeClassName: @"BWRawPath"
asClassName: @"BWSymbol"];
// No need to [super load] - the superclass +load has already
// been invoked automatically by the runtime.
} // load
Note that this won't help you if you're wanting to rename something that was added via encodeValueOfObjCType:. You'll have to write some code to unarchive your data using the old @encode(oldName) and then re-archive it as @encode(newName)
(Thanks to Greg Miller for spotting an error in this quickie)
- (NSData *) dataRepresentationOfType: (NSString *) aType
{
NSData *data;
data = [NSArchiver archivedDataWithRootObject: group];
return (data);
} // dataRepresentationOfType
- (BOOL) loadDataRepresentation: (NSData *) data
ofType: (NSString *) aType
{
// replace the moedl
[group release];
group = [NSUnarchiver unarchiveObjectWithData: data];
[group retain];
// make any associations to hook the new data into your document
return (YES);
} // loadDataRepresentation
NSCoding protocol.
- (void) encodeWithCoder: (NSCoder *) coder
{
[coder encodeInt: x forKey: @"x"];
[coder encodeInt: y forKey: @"y"];
[coder encodeObject: color1 forKey: @"color1"];
[coder encodeObject: color2 forKey: @"color2"];
[coder encodeInt: direction forKey: @"direction"];
} // encodeWithCoder
- (id) initWithCoder: (NSCoder *) coder
{
if (self = [super init]) {
x = [coder decodeIntForKey: @"x"];
y = [coder decodeIntForKey: @"y"];
color1 = [coder decodeObjectForKey: @"color1"];
color2 = [coder decodeObjectForKey: @"color2"];
direction = [coder decodeIntForKey: @"direction"];
}
return (self);
} // initWithCoder- (id) initWithCoder: (NSCoder *) aDecoder
{
if (self = [super init]) {
// or else [super initWithCoder: aDecoder] if your superclass
// supports it. NSObject does not.
// decoding a C type
[aDecoder decodeValueOfObjCType: @encode(int)
at: &itemCount];
// decoding an array of C structs
if (items != NULL) {
free (items);
}
items = malloc (sizeof(BWRawPathItem) * itemCount);
int i;
for (i = 0; i < itemCount; i++) {
[aDecoder decodeValueOfObjCType: @encode(BWRawPathItem)
at: &items[i]];
}
// decoding an object
name = [aDecoder decodeObject];
[name retain];
// any other random initializations that don't come from
// the encoder
fooby = -1;
}
return (self);
} // initWithCoder- (void) encodeWithCoder: (NSCoder *) aCoder
{
// [super encodeWithCoder:] if you inherit from a class
// that supports encoding. NSObject does not.
// encoding a C type
[aCoder encodeValueOfObjCType: @encode(int)
at: &itemCount];
// encoding an array of C structs
int i;
for (i = 0; i < itemCount; i++) {
[aCoder encodeValueOfObjCType: @encode(BWRawPathItem)
at: &items[i]];
}
// encoding an object
[aCoder encodeObject: name];
} // encodeWithCoder