33: Very simple NSDocument example of creating and reading a plain text file using Objective-C

i. Create the project and set the Universal Type Identifier for a Plain Text file type

Create a new XCode Cocoa Project, call it 033_NSDocument and make it a document-based application.

Now set the Universal Type Identifier (UTI) for the type of file to read and to output. Here it will be a public.plain-text file type.

In the XCode window, in the Groups & Files collumn, open the Targets group, select 033_NSDocument and click on the "i" info button. This brings up the Info panel. Choose the Properties tab. We're now going to replace the first line of the Document Types table with our own values. In the Name collumn, replace the string "DocumentType" by "Plain Text". Set the UTI entry to "public.plain-text". Delete the question marks "????" in the Extensions collumn and the OS TYpes collumn. Leave the Class collumn as it is. Store type is "Binary", Role is "Editor" and leave the Package check box unchecked.

Set the Universal Type Identifier in Document Types to public.plain-text

Set the Universal Type Identifier in Document Types to public.plain-text

ii. Modify the dataOfType: error: method in NSDocument.m so it writes some text when we do Flie->Save or File->Save As in the application.

- (NSData *)dataOfType:(NSString *)pTypeName error:(NSError **)pOutError { NSDictionary * zDict; if ([pTypeName compare:@"public.plain-text"] == NSOrderedSame ) { zDict = [NSDictionary dictionaryWithObjectsAndKeys: NSPlainTextDocumentType, NSDocumentTypeDocumentAttribute,nil]; } else { NSLog(@"ERROR: dataOfType pTypeName=%@",pTypeName); *pOutError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL]; return NULL; } // end if NSString * zString = [NSString stringWithString: @"Mary had a little Lamb,\nIts fleece was white as snow\n"]; NSData * zData = [zString dataUsingEncoding:NSASCIIStringEncoding]; return zData; } // end dataOfType

Compile and run. Do File->Save As. Save the file as LittleLamb.txt and take a Quick Look inside it.

Take a quick look at the NSDocument output

Take a Quick Look at the NSDocument output

iii. Modify the readFromData: ofType: error: method so as to read in public.plain-text documents when we do File->Open.

- (BOOL)readFromData:(NSData *)pData ofType:(NSString *)pTypeName error:(NSError **)pOutError { if ([pTypeName compare:@"public.plain-text"] != NSOrderedSame) { NSLog(@"** ERROR ** readFromData pTypeName=%@",pTypeName); *pOutError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL]; return NO; } // end if NSDictionary *zDict = [NSDictionary dictionaryWithObjectsAndKeys: NSPlainTextDocumentType, NSDocumentTypeDocumentAttribute, nil]; NSDictionary *zDictDocAttributes; NSError *zError = nil; NSAttributedString * zNSAttributedStringObj = [[NSAttributedString alloc]initWithData:pData options:zDict documentAttributes:&zDictDocAttributes error:&zError]; if ( zError != NULL ) { NSLog(@"Error readFromData: %@",[zError localizedDescription]) return NO; } // end if NSLog(@"%@",[zNSAttributedStringObj string]); return YES; } // end readFromData

Build and Run. Do File->Open Recent and open the LittleLamb.txt file. In the Console window you will see 'Mary had a little Lamb, Its fleece was white as snow'

If you want to download the code

Click the Download Link to obtain 033_NSDocument.zip file of this whole OS X 10.6 Snow Leopard program.

Download 033_NSDocument.zip (2.3 MB)

Please send me your comments

If you include your e-mail I may reply!  

Page last modified: 18:58 Sunday 12th. May 2013

Julius Guzy

Paintings & Drawings

  • Link to Sketch for paintings of the genocide in Rwanda represented as a large Falla in the Passeo de las Germanias in Gandia