PDA

View Full Version : Saving application state




jsnuff1
Jul 29, 2008, 12:52 PM
Im having a little trouble figureing out how to save simple instance variables to a file in order to save my application state.

I have figured out how to read and write data with Archivers, but this requires objects to be sent to the data archiver via an Array.

The only thing i need to do to save my application state is save two double instance variable and one boolean instance variable and them simply reinitialize them when the application is run again.

How would i go about saving these double and boolean values with an archiver?



jsnuff1
Jul 29, 2008, 02:28 PM
well i cheated and just converted the doubles and bool value into an NSString object and put those into an array and sent the array to the archiver.

Just more work when i read the strings i need to convert them back to double and bool.

Any better way to do this?

Taum
Jul 29, 2008, 04:19 PM
You should convert them into NSNumber (see initWithDouble: and initWithBool: ). Also, your custom objects can be archived if they comply with the NSCoder protocol.

See http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/Archiving.html for more info, this may sound complex but is actually very easy to implement.

louden
Jul 29, 2008, 06:28 PM
That's what NSDecimalNumber and NSNumber is for!

You just need to get used to NSDecimalNumber math. It's long-winded. That and I think it leaks.

jehiah
Jul 29, 2008, 10:12 PM
Im having a little trouble figureing out how to save simple instance variables to a file in order to save my application state.


Another option is to save your application state in a sqlite database.

I've found FMDB (http://gusmueller.com/blog/archives/2008/06/new_home_for_fmdb.html) to be a good library for accessing sqlite databases. You can also check out the SQLiteBooks example application from apple for an example of that.

Sbrocket
Jul 29, 2008, 11:28 PM
Another option is to save your application state in a sqlite database.

I've found FMDB (http://gusmueller.com/blog/archives/2008/06/new_home_for_fmdb.html) to be a good library for accessing sqlite databases. You can also check out the SQLiteBooks example application from apple for an example of that.

Err, an sqlite database is overkill to store three variables. Use NSUserDefaults. As long as its a small amount of data, sqlite is way more work than you need.