Re: Saving information to a file
Re: Saving information to a file
- Subject: Re: Saving information to a file
- From: "Louis C. Sacha" <email@hidden>
- Date: Mon, 22 Dec 2003 13:51:50 -0800
Hello...
For the variables in your controlling class that take care of system
behaviors and preferences, in Cocoa programs the standard way to
store that info is using the NSUserDefaults class, which handles the
details of saving the info to disk and retrieving it automatically.
The documentation in the Foundation reference docs is good, and I
believe there are some additional examples in the /Developer folder
(see end of email ) that covers the use of NSUserDefaults to save
preferences for a program.
For the rest of your data:
If your program needs to maintain a single data file, in other words
it always works on the same set of data, and you have only a small
amount of data stored in your custom class, you could save the
NSStrings and floats using NSUserDefaults in addition to the
program's preferences. This would not be appropriate if your data can
grow to be fairly large or you are making a document style
application which needs to work with multiple sets of data.
The second possiblility is to use NSArchiver or NSKeyedArchiver, as
you mentioned. Using NSArchiver is very much like just writing out
your data to a file, once you have your NSArchiver instance set up,
you just write out all of your data in a specific order using the
encode... methods, and then later you can read it back in using the
decode... methods (most of the methods are documented in NSCoder).
When using NSArchiver, you need to have a fixed data structure, since
it requires that you always read and write the data in the exact same
order. NSKeyedArchiver offers the ability to store your data in a
more dynamic and compatible way, since you can read any part of the
archived data in any order, although sometimes it can be overkill
depending on how your data is structured. After you encoded your
information using either NSArchiver or NSKeyedArchiver, then you
would write the archiver's data object to a file using the methods in
NSData. To get it back, you would use the file reading methods in
NSData then add the NSData object to the appropriate unarchiver class
(NSUnarchiver or NSKeyedUnarchiver) and read the data back out.
A third way would be to store all of the floats (in NSValue or
NSNumber containers) and NSStrings in an NSDictionary or NSArray, and
then use their built in methods to read/write the structure out to a
file. This will work as long as all of your data in the class is made
up of the standard plist objects (NSString, NSData, NSDictionary,
NSArray, NSNumber...). You would need to decide on a structure (if
using an NSArray) or a set of keys (for an NSDictionary), and then
you would use them to read/write your data.
It's always a good idea to dig around in the Examples folder and the
Tasks and Concepts folder to look for more information and sample
code:
/Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/Archiving/
/Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/UserDefaults/
/Developer/Examples/AppKit/UserDefaults/
Hope that helps,
Louis
I want to retain information from my program by writing it to a file and then
retrieving it (as any good programmer would). I would use NSArchiver but my
situation is complicated.
I have a class that has information made up of NSStrings and floats
Furthermore, I have loose variables in my main controlling class such as ints
that take care of system behaviors and preferences.
Can't I just create a file, open it, and write my stuff out ala c++
and cout <<?
Or is NSArchiver still the way and if so can you please include some
links that
may help me.
Thanks in advance,
jay
_______________________________________________
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.