I am adding a NSString to an NSArray and reading it back in an other Tableview which works flawless.
In my Detailview:
NSString *documentDirectory = [self applicationDocumentsDirectory];
NSString *path = [documentDirectory stringByAppendingPathComponent:@"Favs.plist"];
NSArray *Array = [[NSArray alloc] initWithObjects:self.title, nil];
[Array writeToFile: path atomically:YES];
In my FavTableView:
NSString *documentDirectory = [self applicationDocumentsDirectory];
NSString *path = [documentDirectory stringByAppendingPathComponent:@"Favs.plist"];
tempArray = [[NSArray alloc] initWithContentsOfFile:path];
array = [[NSMutableArray alloc] initWithArray:tempArray copyItems:YES];
NSLog(@"array is: %d", [array count]);
But if I want to load, in a different Detail view, another String to my Array, it overwrites the old value. How do I avoid that?
You’d batter to create NSDictionary or NSArray, that will contain your arrays and write to file that global dictionary or array. If you need to append arrays one by one, you’ll need to go through the following steps:
1. Initialize Dictionary with contents of existing file
2. Create mutable copy of the dictionary, created at step #1
3. Add new array to the dictionary
4. Write updated dictionary to the file
If global array better fits your needs than global dictionary, the steps are the same, but use array instead of dictionary =)