Re: Saving "objects" & opening them up in a non-document based App
Re: Saving "objects" & opening them up in a non-document based App
- Subject: Re: Saving "objects" & opening them up in a non-document based App
- From: Brian Ganninger <email@hidden>
- Date: Thu, 15 Jan 2004 05:05:13 -0600
Simple answer: NSArchiver & NSUnarchiver. Full documentation is already
on your system (I recommend Cocoa Browser to find it easily enough)
As long as you conform to the NSCoder protocol within your object you
can easily write the object out into a file of whatever path you set
"example/entry.today" and then unarchive the same path into an object
in memory.
Example code: (reading & writing go in controller, encode/init goes in
object)
// reading the file in
NSMutableArray *tempArray = [NSUnarchiver
unarchiveObjectWithFile:@"/Library/Application Support/Your App/Support
File.helpme"];
// writing the file out
[NSArchiver archiveRootObject:tempArray toFile:@"/Library/Application
Support/Your App/Support File.helpme"];
// encoding methods
- (id)initWithCoder:(NSCoder *)coder // for loading
{
self = [super init];
[coder decodeValueOfObjCType:"i" at:&intVariableInClass]; // see
below
[self setThing:[coder decodeObject]];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder // for saving
{
[coder encodeValueOfObjCType:"i" at:&intVariableInClass];
// note: & indicates a reference to an-class variable of a base
type, not object
[coder encodeObject:[self thing]]; // for objects
}
That should get you well on your way to saving nirvana. (replace as per
your actual programming models, of course :)
HTH,
Brian
On Jan 15, 2004, at 4:18 AM, Aaron Boothello wrote:
I have a Cocoa application (non document based) and i want to save the
data in a class i created (NSObject:myObject) to a file and be able to
open it back up. ive browzed the net but all i find is stuff for
document based apps. i really need some help, sample code or tutorials
cause i have no idea where to begin.
Aaron Boothello.
_______________________________________________
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.
_______________________________________________
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.