This article shows you how to read text from a file into an NSTextView and how to write text to a file from an NSTextView.
Reading Text From a File
Writing To a Text File
To read text from a file, you first must determine the format of the text. To illustrate how this is done, consider an object of the custom class Controller. A Controller object is responsible for opening and closing files. It stores an NSTextView object and declares a variable that records the format of the text that it reads in. Here’s the interface declaration:
Listing 1 Determining the text format
#import <AppKit/AppKit.h> |
typedef enum _dataFormat { |
Unknown = 0, |
PlainText = 1, |
RichText = 2, |
RTFD = 3, |
} DataFormat; |
@interface Controller : NSObject |
{ |
DataFormat theFormat; |
NSTextView *theTextView; |
} |
- (void)openFile:(id)sender; |
- (void)saveFile:(id)sender; |
@end |
Now, the Controller object’s openFile: method can be implemented like this:
Listing 2 Reading the text from a file
- (void)openFile:(id)sender |
{ |
NSOpenPanel *panel = [NSOpenPanel openPanel]; |
if ([panel runModal] == NSOKButton) { |
NSString *fileName = [panel filename]; |
if ([[fileName pathExtension] isEqualToString:@"rtfd"]) { |
[theTextView readRTFDFromFile:fileName]; |
theFormat = RTFD;} |
else if([[fileName pathExtension]isEqualToString:@"rtf"]) { |
NSData *rtfData = [NSData dataWithContentsOfFile:fileName]; |
[theTextView replaceRange: |
NSMakeRange(0, [[theTextView string] length]) withRTF:rtfData]; |
theFormat = RichText; |
} else { |
NSString *fileContents = [NSString |
stringWithContentsOfFile:fileName]; |
[theTextView setString:fileContents range:NSMakeRange(0, |
[[theTextView string] length])]; |
theFormat = PlainText; |
} |
} |
return; |
} |
The openFile: method checks the file name returned by the Open panel for the extensions “rtfd” or “rtf” and uses the appropriate means of loading data for each type. Files having any other extension are loaded as plain text. Note that the Controller object records the format of the loaded data in its theFormat variable. This information is used to determine how the file should be saved, as discussed in the next section.
Depending on the format of an NSTextView object’s text, you use slightly different approaches to write the text to a file. For plain text, you extract the contents of the NSTextView as an NSString object and use the NSString method writeToFile:atomically: to write the data to disk. RTF text is treated similarly, except that the contents is extracted as an NSData object. Easiest of all is RTFD data, which the NSTextView itself knows how to write to a file:
Listing 3 Writing text to a file
- (void)saveFile:(id)sender |
{ |
NSSavePanel *panel = [NSSavePanel savePanel]; |
switch (theFormat) { |
case PlainText: |
[panel setRequiredFileType:@""]; |
if ([panel runModal] == NSOKButton) { |
[[theTextView string] writeToFile:[panel filename] |
atomically:YES]; |
} |
break; |
case RichText: |
[panel setRequiredFileType:@"rtf"]; |
if ([panel runModal] == NSOKButton) { |
[[theTextView RTFFromRange: |
NSMakeRange(0, [[theTextView string] length])] |
writeToFile:[panel filename] atomically:YES]; |
} |
break; |
case RTFD: |
[panel setRequiredFileType:@"rtfd"]; |
if ([panel runModal] == NSOKButton) { |
[theTextView writeRTFDToFile:[panel filename] |
atomically:YES]; |
} |
break; |
default: |
NSRunAlertPanel(@"Save Error", |
@"Couldn’t save file (unknown data format).\n", |
nil, nil, nil); |
break; |
} |
return; |
} |
Last updated: 2004-02-10