Women in Technology

Hear us Roar



Article:
  The Cocoa Controller Layer
Subject:   Save and Loading the Array-Data
Date:   2004-04-15 05:51:49
From:   kool
Response to: Save and Loading the Array-Data

Scott Anguish just showed me how to do this:


There are two ways to connect data to an NSArrayController.. by setting the content, or by binding the contentArray item to the object..


I recommend keeping the array value (in your case, the contents of "MyContent" as a variable of the Document, and then bind the array controller to the File's Owner MyContent key.


Anyways, that's partly moot in this case. loadDataRepresentation:ofType: is called before the nib is loaded. So the nib doesn't have an NSArrayController yet. You need to wait until the windowControllerDidLoadNib: is done.


So, my fix would be


add a "NSMutableArray *myContent" to your document class, and accessors for it.
remove the connection to 'content' in IB
bind the array controller's contentArray to that variable on File's Owner in the nib



- (NSData *)dataRepresentationOfType:(NSString *)aType
{
NSKeyedArchiver *archiver;
NSMutableData *data = [NSMutableData data];


archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:[self myContent] forKey:@"MyContent"];
[archiver finishEncoding];


return data;
[archiver release];
}


- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
NSMutableArray *mutArray;
NSKeyedUnarchiver *unarchiver;


unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
mutArray = [unarchiver decodeObjectForKey:@"MyContent"];


[self setMyContent:mutArray];


[unarchiver finishDecoding];
[unarchiver release];
return YES;
}