One Quickie


Archiving a document using keyed archiving (NSCoder->General)
This uses the new Tiger NSDocument load/store. For real code you'll want to handle errors and create an appropriate NSError object. This also saves the document in an XML format.
- (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



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com