We've expanded our news coverage and improved our search!
for the latest or search for all things across O'Reilly!
The Cocoa Controller Layer
Save and Loading the Array-Data
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
NSMutableData *data = [NSMutableData data];
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:[self myContent] forKey:@"MyContent"];
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
mutArray = [unarchiver decodeObjectForKey:@"MyContent"];