• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSDocument: how to save document as a simple text file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSDocument: how to save document as a simple text file


  • Subject: NSDocument: how to save document as a simple text file
  • From: Gilles Celli <email@hidden>
  • Date: Fri, 26 Apr 2002 18:20:24 +0200

Hi,

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.

But how do I get the "path" when I choose to save from the menu
With "File -> Save as..." ?

And where should I put this method in
Of (NSData *)dataRepresentationOfType:(NSString *)aType ?

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

}


Cheers,


>>If the contents of your document can be expressed as a simple NSString,
>>you can write that to a file with writeToFile:atomically:. You can get
>>the string in a text object by doing [textView string].

>>Or you can be more explicit about the encoding by converting to an
>>NSData first, then writing that. This is twice as much work, but the
>>preferred way:

>> NSData *data = [string dataUsingEncoding: yourEncoding];
>> if (data) [data writeToFile: fullPath atomically: YES];

>>You can use UTF-8, or MacRoman, or whatever's appropriate for the
>>clients who will load this document.
>>Note that MacRoman conversion might fail (that is, you will get back a
>>nil data) if the string contains characters that cannot be expressed in
>>MacRoman. If it's all ASCII or Western European, it should work.

> Well I have written a document based Cocoa app which reads
> data from a thermometer (via a USB to Serial Adapter).
>
> Now I just want to save the data in text format instead of RTF,
> So I can open it in Bbedit or read it in my favorite graph app.
> I know how to save it in RTF though.


* * Gilles Celli
* * European Center for Geodynamics and Seismology
* ECGS * 19, rue J. Welter, L-7256 Walferdange, Gd Duchy of Luxembourg
* * Tel: (+ 352) 33.61.30 /\ Fax: (+ 352) 33.61.29
* * email@hidden __/ \ /\ ___ Web Page: www.ecgs.lu
\/ \/
_______________________________________________
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.

  • Follow-Ups:
    • Re: NSDocument: how to save document as a simple text file
      • From: mmalc crawford <email@hidden>
    • Re: NSDocument: how to save document as a simple text file
      • From: Ondra Cada <email@hidden>
  • Prev by Date: Sometimes a method gets called, sometimes it doesn't.
  • Next by Date: Objective-C coding Stndards
  • Previous by thread: Re: NSDocument: how to save document as a simple text file
  • Next by thread: Re: NSDocument: how to save document as a simple text file
  • Index(es):
    • Date
    • Thread