vendredi 8 mai 2015

Saving and Loading an NSMutableArray

I want my app to work so that when a user presses a "save" button, an NSMutableArray of strings (the array is called "names") inputted from a textfield will be saved. Naturally I also then want to be able to load the NSMutableArray any time I close/reopen the app.

Right now my save button is an IBAction "save". So in my implementation file I have:

- (IBAction)save:(id)sender
{ 
    NSArray *paths =  NSSearchPathforDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0]; 
    NSString *fileName = [NSString stringWithFormat:@"%@/myArray", docDir]; 
    [NSKeyedArchiver archiveRootObject:names toFile:fileName];
}

First of all, does this seem like it should work? Because multiple times my app crashed when I tried to then press the save button. Second am I right to be creating the file path in the IBAction? Or should I be creating it somewhere else (e.g. under viewDidLoad)?

Second, how and where should I be loading my saved NSMutableArray ("names")?

Many thanks!

Aucun commentaire:

Enregistrer un commentaire