Keyed archiving
Keyed archiving
- Subject: Keyed archiving
- From: Andrew Merenbach <email@hidden>
- Date: Sat, 14 Sep 2002 13:21:58 -0700
I would like to implement keyed archiving for a program's data. I'm
having a bit of trouble with dataRepresentationOfType:. How should I
convert my code? (The current code looks like this:)
- (void)loadDocumentWithInitialData {
// Decode data we previously archived. Format is an array of
strings followed by text storage for the text view.
NSUnarchiver *unarchiver = [[[NSUnarchiver alloc]
initForReadingWithData: dataFromFile] autorelease];
NSArray *archivedInputFieldStrings = [unarchiver decodeObject];
NSTextStorage *archivedTextStorage = [unarchiver decodeObject];
// Populate the text fields, and text view with data that was
unarchived.
if ([archivedTextStorage length]) [quotientTextView insertText:
archivedTextStorage];
//[quotientTextView setEditable:NO];
[self setDividend: [archivedInputFieldStrings
objectAtIndex:INDEX_DIVIDEND]];
[self setDivisor: [archivedInputFieldStrings objectAtIndex:
INDEX_DIVISOR]];
[self setScale: [archivedInputFieldStrings objectAtIndex:
INDEX_SCALE]];
// Make sure the document doesn't think it is dirty. Calling
insertText: above could have made the doc think it was dirty.
//[[self undoManager] removeAllActions];
[dataFromFile release];
dataFromFile = nil;
}
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
// Insert code here to write your document from the given data.
You can also choose to override -fileWrapperRepresentationOfType: or
-writeToFile:ofType: instead.
// Archive data in the format loadDocumentWithInitialData expects.
NSMutableData *data = nil;
if ([aType isEqualToString: XQUOTIENT_DOCUMENT_TYPE]) {
NSArray *inputFieldStrings = [NSArray arrayWithObjects: [self
dividend], [self divisor], [self scale], nil];
NSArchiver *archiver = [[[NSArchiver alloc]
initForWritingWithMutableData: [NSMutableData data]] autorelease];
[archiver encodeObject: inputFieldStrings];
[archiver encodeObject: [quotientTextView textStorage]];
data = [archiver archiverData];
}
return data;
}
Take care,
Andrew Merenbach
_______________________________________________
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.