views:

131

answers:

3

I want to allow my user to store custom phrases, to be displayed in an editable UITableView.

What's a quick and dirty to store these strings?

I'm fairly new at iPhone development. I know about Core Data, but not how to use it. i would stay away form that just for this particular project if possible. Are PLIST files a possibility here?

Sample code is appreciated.

Thank you.

+2  A: 

Use NSUserDefaults. There's some code over in this question.

progrmr
I thought that NSUserDefaults is good for settings. I'm looking to store an undetermined number of strings.
Moshe
It's just a key-value store. An array of strings is a valid thing to store there.
Chuck
@Chuck - Thanks.
Moshe
+1  A: 

You probably want NSDefaults; check out the User Defaults Programming Topics guide.

David Gelhar
Can I use NSUserDefaults to store NS(Mutable)Arrays?
Moshe
Yes, you can store any type can be serialized to a plist. plist-compatibile objects (such as arrays of strings) can be stored directly. Any other that conforms to `NSCoding` can by stored by using an `NSArchiver` to serialize to `NSData` first.
David Gelhar
+1  A: 

If you want to use plists this is the quickest way (I think) to go. There are other ways to do it with NSCoding to encode your data to a file, but you would probably need a custom data model class, this is more suited to saving more random things that wouldn't fit into a prescribed data model. Also, as has been pointed out, NSUserDefaults is also an option

To save your work:

-(void)applicationWillTerminate:(NSNotification *)notification {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *filePath = [documentsDirectory stringByAppendingString:@"info.plist"];
   NSMutableArray *array = [[NSMutableArray alloc] init];
// Add the objects you want to save to the array here ex: [array addObject:]
   [array writeToFile:filePath atomically:YES];
   [array release];  }

And to recover your saved file:

- (void)viewDidLoad {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *filePath = [documentsDirectory stringByAppendingString:@"info.plist"];
   if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
       NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
    // Set your vaiables here in the order that you saved them ex. var = [array objectAtIndex:]
       [array release];
   }
   UIApplication *app = [UIApplication sharedApplication];
   [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(applicationWillTerminate:) 
                                             name:UIApplicationWillTerminateNotification 
                                           object:app];

    [super viewDidLoad];}

Basically, when your application is about to quit you create and array of the objects you want to save and write them to the file, and when you application starts again you read that file back into an array and assign your variables based on the order in which you saved them. Then you set it up so when your application sends its UIApplicationWillTerminateNotification it executes your code to save your (presumably) modified variables back into a file.

thelaws
+1 for code and excellent answer. The only flaw here (not mentioned in the OP, my bad) is that this data requires a restart of the app to be used, based on the way my app works. NSUserDefaults is probably better here. Thanks, though.
Moshe
No problem, NSUserDefaults and CoreData are probably the real ways to go to store you information anyway. This method is pretty simple, it works so naturally it's great, but learning coredata is pretty important I think. (I've only started to really learn it recently myself)
thelaws