Lefora Free Forum
login join
Loading
330 views

Code for Saving a Document

Page 1
1–5
novice - member
25 posts

sup i got a nice program that i am working on it almost done. couple things missing.
the most important thing that is missing is that i need to save the file. and than open it to where it was before.. (you know how saving works) 

but i would like to know what the code is soo i can save my program. or how to do it. thanks.

superstar - member
234 posts

Well there are lots of possible answers here.  If you've build a document then writing your objects to file is very straightforward.  I recommend that you get a copy of Aaron Hillegass' book and he deals with this (Chapter 10 - Archiving) And reading/writing preference files (Chapter 15) and all manner of good stuff.

If you've build something more 'vanilla' (not based on the NSDocument class) then remember Obj/C is C (and Obj/C++ is C++) so you have 100% of the platform library at your disposal.  So fopen/fread/fwrite/fclose C library calls are available, and the std STL template classes in C++.

The NSData class provides a "initWithContentsOfFile" and "writeToFile" methods for binary I/O.  And foundation classes such as NSString and NSDictionary have methods to read and write disk files.

Just to give you even more choice (and confuse you totally), you can read/write XML, read/write SQLite databases and even use HTTP posts, gets and puts to push/pull your data over the web.

And I wouldn't be surprised if there were even more choices that I'll remember after I've pressed the "Submit Reply" button. 

__________________
novice - member
25 posts

@clanmills

i have that book. Cocoa Programming for Mac OS X - 3rd-Edition.
dont really like it. i rather learn cocoa visually and have a little logic to it.

but i figured it out. here is the code for save a "application" not a document-based project cause that has the code built into the NSDocument Object already i believe.

saveTest OBJ-C Header file (interface)


#import <Cocoa/Cocoa.h>


@interface saveTestAppDelegate : NSObject <NSApplicationDelegate> {

    NSWindow *window;


IBOutlet NSTextField *aTextField;

}


@property (assign) IBOutlet NSWindow *window;


-(IBAction) save: (id)sender;


@end



saveTest OBJ-C Class file (implementation)



#import "saveTestAppDelegate.h"


@implementation saveTestAppDelegate


@synthesize window;


-(IBAction)save: (id)sender{

NSSavePanel *savePanel = [NSSavePanel savePanel];

SEL sel = @selector(savePanelDidFinish:returnCode:contextInfo:);

[savePanel beginSheetForDirectory:@"~/Documents" 

file:@"file.txt" 

modalForWindow:window

modalDelegate:self 

didEndSelector:sel 

contextInfo:nil];

}


-(void)savePanelDidFinish: (NSSavePanel *)sheet 

returnCode:(int)returnCode

contextInfo:(void *)context {


if (returnCode == NSOKButton) {

[NSArchiver archiveRootObject:aTextField toFile:[sheet filename]];

}

}



@end


hope this helps whoever had the sam problem. 
im still a Cocoa Newb.

superstar - member
234 posts

Congratulations, that looks about right to me.

I'm sorry you don't like the book - many people do.  I did purchase an O'Reilly book (Learning Cocoa), however it's rather old and out of date.  I was a little disappointed with "Programming with Objective-C 2.0".  It's a good text on the Obj/C language - however it doesn't really deal with Cocoa.

With the iPhoneSDK (and soon the iPad), Cocoa is becoming more 'mainstream' and there are more books appearing all the time from publishers such as Wrox.

If you find a book that you like, I hope you'll share that on the forum so that others can profit from your effort.

Thanks for your contribution to the forum.  This is very helpful.

__________________
novice - member
25 posts

i belive the the way you open stuff works the same but instead of NSSavePanel it would be NSOpenPanel. :)

Page 1
1–5

Locked Topic


You must be a member to post in this forum

Join Now!