| Article: |
The Cocoa Controller Layer | |
| Subject: | Save and Loading the Array-Data | |
| Date: | 2004-04-12 05:55:02 | |
| From: | AlexKid | |
|
Thats a nice article of a great technologie. But could somebody show me the way to save the data colectet in the text view to a file and to load it again. I have some idees but I don t know realy how to do this. I hope somebody can show me a tuturial or an example program. Or tell me what i have to do.
|
||
Showing messages 1 through 1 of 1.
-
Save and Loading the Array-Data
2004-04-15 05:51:49 kool [View]



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;
}