4

I am working on a code which requires me to store 60*4 boolean values, the titles for these values are being stored in a plist. I require to manipulate the boolean values at runtime and couldn't find a way to update the plist file easily..also using sqlite database for storing the boolean values becomes hectic for such large amount of data...Is there any simple way through which I can store and retrieve these values easily both at runtime and after the application starts?

iDeveloper
  • 940
  • 1
  • 10
  • 45
Snehal
  • 154
  • 1
  • 13
  • Have a look at this SO question [Possible to save an integer array using NSUserDefaults on iPhone?](http://stackoverflow.com/questions/350848/possible-to-save-an-integer-array-using-nsuserdefaults-on-iphone) – epatel Feb 26 '09 at 13:19

3 Answers3

7

I don't mean to be a heretic, but there's a simple rule for cases like this: premature optimization is the root of all evil.

60*4 is only 240 booleans. Even if you somehow manage to store them in the worst possible way and take 1k per boolean, that's still only 240k. As long as that's storage rather than RAM, who cares? Why not start with the simplest possible way and fix it when something comes to you later? SQLite would be perfectly fine for this.

If you're close to shipping and have identified this as a problem, by all means ignore this answer. :)

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
  • I have to agree. Worrying about 240 booleans is not the same as worrying about 240000 booleans. You try to pack them and the pack/unpack code will use up more space than the booleans themselves. – Nosredna Feb 26 '09 at 19:08
  • Actually for convience we had taken it 4 times but it can go to 100 also...so it will be 60*100 hence i was looking for some other way – Snehal Mar 02 '09 at 05:12
  • Well, storing 100 sets of 60 booleans (6000 total) in a sqlite3 database with an index and no optimization takes 150k. If the sets are always the same size, you could store them as strings of 60 bytes (+length) in indivdual records, but I think storing them individually is the better way to go. – Steven Fisher Mar 03 '09 at 20:31
2

While its going to be much easier to use NSArray or NSMutableArray as mentioned above, you could look at using the standard C++ vector class. AFAIK this is very space-efficient wrt. the allocation of memory.

corvi42
  • 21
  • 1
1

You could use the NSData method of storing the boolean array, but you could also just let cocoa do it naturally:

NSArray* arrayOfBools; // array of 240 NSNumbers, each made with [NSNumber numberWithBool:NO];

then

[[NSUserDefaults standardUserDefaults] setObject:arrayOfBools forKey:@"MyNameForThe240"];

Retrieve them:

NSArray* savedBools = [[[NSUserDefaults standardUserDefaults] objectForKey:"MyNameForThe240"];

You will likely want them in a mutable array:

NSMutableArray* the240ThatCanBeEdited = [NSMutableArray arrayWithArray:savedBools];

Then on quit, save them out with the

[[NSUserDefaults standardUserDefaults] setObject:the240ThatCanBeEdited forKey:@"MyNameForThe240"];
Emond
  • 50,210
  • 11
  • 84
  • 115
Tom Andersen
  • 7,132
  • 3
  • 38
  • 55