Re: Reading/Writing Data From NSTableView
Re: Reading/Writing Data From NSTableView
- Subject: Re: Reading/Writing Data From NSTableView
- From: mmalcolm crawford <email@hidden>
- Date: Fri, 28 Jan 2005 23:40:36 -0800
On Jan 28, 2005, at 11:06 PM, M. Uli Kusterer wrote:
Shouldn't be too hard to archive in a generic way, though. Just create
a subclass of NSDocument (e.g. UKArchivingDocument) and give it an
IBOutlet NSObjectController * controllerToArchive;
And then implement the following methods:
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
return [NSArchiver archivedDataWithRootObject:
[controllerToArchive content]];
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
id content = [NSUnarchiver unarchiveObjectWithData: data];
[controllerToArchive setContent: data];
return YES;
}
Shouldn't be much more to do if your objects comply to NSCoding, which
most of the Foundation objects do.
Certainly if you're just using Foundation classes, this will work. If
you want to write a single object to a file there are also methods --
readily found in the documentation -- for doing so (see for example
NSArray and NSDictionary's writeToFile:atomically:). If you have any
custom objects, however, you will almost certainly need to to look at
NSCoding et al.
As a further point, in an NSDocument subclass it is likely that the
model is a property of the document, so the code will typically look
more like this:
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
return [NSKeyedArchiver archivedDataWithRootObject:bookmarksArray];
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
[self setBookmarksArray:[NSKeyedUnarchiver
unarchiveObjectWithData:data]];
return YES;
}
(excerpt from the Bookmarks example at
<http://homepage.mac.com/mmalc/CocoaExamples/controllers.html>).
See also
<http://developer.apple.com/documentation/Cocoa/Conceptual/Documents/
index.html>, in particular "Implementing a Document-Based Application".
mmalc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden