views:

481

answers:

3

I have an NSMutableArray with 24 strings.

I need to save this data if a user gets a call or quits the application.

I have been looking into many examples but for some reason can’t seem to determine the best way to save the data.

The 24 strings correspond to 24 buttons and their state. When a button is clicked, it displays the corresponding Array info for that buttons tag (0 – 23). What I would need to retain is if 10 buttons where clicked and their data shown, how/what would be the best way of retaining this data so it can be reloaded when the app starts?

I am thinking I would need to store:

Button Tag, Buttons corresponding Array value,
Button state (whether it has clicked and value is show or not)

I would store this data on exit of the application and then when app is started again, I would determine if this stored data exists, if so populate the array and examine the button states to determine if it had already been shown and if so, set it accordingly. Then when this file was loaded, I would delete the stored data (.DAT file if stored this way). This way if a user quits gracefully, on next start up, it would start a new game.

I have looked at several examples where they store data into a .DAT file but am having problem implementing this….and wondering if this is even the best way.

Any help or thoughts on this is greatly appreciated.

Geo…

+2  A: 

You could save the data to a plist in the Documents directory. If the data is there, load it up, and if not, it would suggest a clean run.

To load the data:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *filePath = [documents stringByAppendingPathComponent:@"buttonState.plist"];
NSMutableDictionary *buttonState = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

To save the data:

[buttonState writeToFile:filePath atomically: YES]; 
Don
+4  A: 

you should be able to store it in NSUserDefaults

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:buttonState forKey:@"buttonState"];
[defaults synchronize];

// Then to get the state back
NSMutableArray* buttonState = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"buttonState"] mutableCopy];
kubi
I do not recommend using the NSUserDefaults to store game state. Also, you will need to call `synchronize` on the defaults after setting values, otherwise they might not be saved to disk.
St3fan
What's wrong with using `NSUserDefaults` for this, St3fan? It is certainly not an uncommon approach.
Johan Kool
`NSUserDefaults` is simply a nice interface for writing plist files to disk for you. So using it or using manual file reading and writing makes little difference. This approach is fine.
Squeegy
+1  A: 

I save my data by using NSKeyedArchiver/NSKeyedUnarchiver, since your NSMutableArray already supports the NSCoding protocol, you can just use

yourArray = [NSKeyedUnarchiver unarchiveObjectWithFile:file];

[NSKeyedArchiver archiveRootObject:yourArray toFile:file];

Edit:

I suppose NSArray's own methods work also

[NSArray writeToFile:]
[NSArray initWithContentsOfFile:]
sylvanaar