0

I'm trying to create a save & load module in my app. So the user can save some specific info and then load it at any point in time. What I'm saving is an array of NSMutableStrings. I'm creating an iPad app so the saved info will be presented in a popover with a TableView.

So, here's the question. I would like to populate that tableView with my saved arrays, and for that I need to store my arrays somewhere. I'm new to objective-c and programming in general, and would like your advice on what's the best way of doing that.

Should I use a plist to store those arrays with Keys, so the keys will be the names that the user introduces when saving and also the name of the tableView cells? or maybe a simple NSMutableDictionary will do?

Please advise.

Sergey Katranuk
  • 1,162
  • 1
  • 13
  • 23

2 Answers2

1

This answer might help you. Using Core Data might be overkill here, and saving to a plist is extremely easy. This is Benoît's answer from the linked question:

NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"yourFile.plist"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:rootObj 
    format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData) {
    [plistData writeToFile:plistPath atomically:YES];
}
else {
    NSLog(@"Error : %@",error);
    [error release];
}

where rootObj is the object you wish to serialize, in this case, your array/dictionary.


Also, if you are going to be read/writing to this data frequently during the course of your app, using a singleton which can make a huge difference performance-wise instead of constantly reading/writing from/to a file. You can load all of your data into the singleton on app-launch and save it upon applicationWillTerminate.

To answer your array/dictionary question, it depends on how you are obtaining your strings and how you want them to be ordered. Sorting an array is extremely easy, as you can see in this answer.

Community
  • 1
  • 1
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
  • Thanks for the reply, Rickay. You've been very thorough. However, I found a tutorial for the same thing and I think it fits my needs better : http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/ Also, thanks for the singleton advice, but for now I'm not so savvy to understand how it works and how I can use it. Probably I'll return to that if I actually encounter the problem. I'll give this way a try and will get back if any questions appear – Sergey Katranuk Feb 05 '12 at 19:23
  • Thanks, I'll need it ;) Approaching the end of developing my very first app, so I'm VERY excited :D – Sergey Katranuk Feb 05 '12 at 19:34
  • Awesome! It's a great feeling. – eric.mitchell Feb 05 '12 at 19:42
0

Sergey, based on your description, I would advise you look into using Core Data for data storage.

https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/coredata/cdprogrammingguide.html

In addition to storing data, Apple has extended the use of Core Data in all sorts of neat ways, including the use of stored data to populate a UITableViewController through the use of NSFetchedResultsController

https://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html

That's it, give that a read and you should be well on your way.

Fang Chen
  • 236
  • 2
  • 14
  • Thank you for the advice gitJelly, but isn't Core Data too complex for such simple operations? I mean, I'm just storing some arrays with strings. – Sergey Katranuk Feb 05 '12 at 19:01