Someone help please?

martinatkinson

Registered
Hello!

I really need someones help! I have a Cocoa Document-based app. It has two NSTextViews and two NSTextFields. I have found out how to write the content of one NSTextView to disk when the user saves the document. I need to know how to save the contents of all four fields to disk and be able to put them in the correct fields when the document is viewed.

Can anyone help or refer me to a person/website that can help!

All help is greatly appreciated, have a great day!

Albert
 
If you don't want to do anything fancy with the file but store those text objects, how about putting them into an NSArray and using NSArchive/NSUnarchive to write/read them?

Writing:
Code:
NSArray *someArray = [ NSArray arrayWithObjects:text,fields,here,nil ];
if( [ NSArchiver archiveRootObject:someArray
                 toFile:@"/tmp/archfile" ] == YES )
   // do something when successful here
else
   // do something when fails here

Reading back:
Code:
NSArray *readArray = [ NSUnarchiver unarchiveObjectWithFile:@"/tmp/archfile" ];

// then just [ readArray objectAtIndex:0 ]
// and :1 :2 etc...
 
Hello!

Thanks for your help!

I am totally lost though, where do I put this code? MyDocument.m? main.m?

One more question, does this code allow the user to specify where to save the file? From what it looks like, your code writes it out to a specific file. I will not be doing any formatting or coloring of the text but I need to be able to alow the user to save the file where they want and create a new, blank file if they want to.

Have a great day!

Albert
 
The toFile: argument on NSArchive and the only argument for the NSUnarchive specify the file to use; I simply used a static string since I'm lazy when it comes to short code examples...just plop an NSString variable there instead, which would be the filename the user specifies wherever you have them specify that.
 
Hello!

Thanks for your help! This doesn't seem to be working with my project though. Sorry about that, I appreciate your willingness to help!

I guess what I am looking for is the code I need to put in the following blocks (in MyDocument.m):

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
}

- (NSData *)dataRepresentationOfType:(NSString *)aType
{
}

- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
}

I have made outlets for the text fields in MyDocument.h and connected them to the fields in Interface Builder. Let me know if you need the names of the outlets.

Thanks and have a great day!

Albert
 
Back
Top