Re: NSDocument: how to save document as a simple text file
Re: NSDocument: how to save document as a simple text file
- Subject: Re: NSDocument: how to save document as a simple text file
- From: mmalc crawford <email@hidden>
- Date: Fri, 26 Apr 2002 10:53:09 -0700
On Friday, April 26, 2002, at 09:20 AM, Gilles Celli wrote:
Several days ago I asked how to save a document as a simple text file
In an NSDocument based Cocoa app.
Well several readers (thanx) pointed out to use the method
writeToFile:path
atomically: to save it as a text file.
Unfortunately it looks lke you were led astray...
But how do I get the "path" when I choose to save from the menu
With "File -> Save as..." ?
You don't. this is one of the benefits of using a *framework*. You
have very little work to do. You don't have to create the Open Panel,
you don't have to figure out the path, all you have to do is supply the
data when you're asked for it --
And where should I put this method in
Of (NSData *)dataRepresentationOfType:(NSString *)aType ?
-- in dataRepresentationOfType: or either of the other NSDocument "save"
methods.
Well this is the code I implemented to save in RTF format when I choose
"File -> Save as.." and it works great.
(Name of the NSTextView is "textView")
// NSDocument
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
// Ok this works to save as RTF format
NSRange range = NSMakeRange(0, [ [textView string] length]);
return [textView RTFFromRange:range];
}
So, by extension:
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
// Ok this works to save as RTF format
NSRange range = NSMakeRange(0, [ [textView string] length]);
return [[textView string] dataUsingEncoding:NSUTF8StringEncoding];
}
The tricky bit is going to be to decide what encoding you should use.
UTF8 is probably a good default.
I'll send you an example app separately. It illustrates the use of
different types in a multi-doc architecture application. It allows you
to open images and text files in the same app, but with different
NSDocument subclasses. If this is something anyone else needs help
with, let me know, and I might get round to writing an article about
it...
mmalc
_______________________________________________
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.