Reputation: 759
On touch up inside
of SAVE button, the following code is executed:
- (IBAction)onSave:(id)sender {
savecount++;
[self saveNumberOfContacts];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:nameTextField.text];
[myArray addObject:phoneTextField.text];
[myArray addObject:addressTextField.text];
[myArray addObject:cityTextField.text];
[myArray addObject:stateTextField.text];
[myArray addObject:zipcodeTextField.text];
[myArray writeToFile:[self saveFilePath] atomically:YES];
}
This creates a single array. I want to know how to dynamically create multiple arrays with the savecount
variable suffixed at end of the array name.
For example, if my savecount
is 3, then myArray1, myArray2, myArray3 should be created.
P.S. savecount
changes its value dynamically.
EDIT: i dont want this method creating a number of arrays every time i call it. See, the user's info is stored in myArray1
when i click save for the first time. Now, the savecount
gets increemented(say,savecount=2). When i enter another user's details and click save, i dont want myArray1
to be overwritten or disturbed; the second user's details must be independently saved in myArray2
.
Upvotes: 0
Views: 1026
Reputation: 2768
I think your problem is the path of save, or same path with appended array data.
If save data to different path:
- (NSString*)saveFilePath{
return [NSString stringWithFormat:@"%@/%@%d.%@", thePathToSave, fileName, savecount, fileType];
}
If save to same path with appended array data:
Replace:
[myArray writeToFile:[self saveFilePath] atomically:YES];
with
NSArray *fileData = [NSArray arrayWithContentsOfFile:[self saveFilePath]];
NSMutableArray *preDatas = nil;
if (preDatas.count == 0) {
preDatas = [NSMutableArray array];
}
else{
preDatas = [NSMutableArray arrayWithArray:fileData];
}
[preDatas addObject:myArray];
[preDatas writeToFile:[self saveFilePath] atomically:YES];
Upvotes: 0
Reputation: 9093
If you are only using these arrays within one method, you could instead store the arrays in an array. This doesn't give you array names such as myArray1, myArray2, etc, but accomplishes the same overall task. Here's an example:
NSMutableArray *myArrays = [[NSMutableArray alloc] initWithCapacity:savecount];
for (NSUInteger i = 0; i < savecount; i++) {
NSMutableArray *newArray = [[NSMutableArray alloc] init];
[myArrays addObject:newArray];
[newArray addObject:nameTextField.text];
// continue adding your objects to newArray
}
Now you can reference your arrays as [myArrays objectAtIndex:0]
...[myArrays objectAtIndex:savecount-1]
.
Upvotes: 1
Reputation: 9836
NSMutableArray *holder;
- (void)viewDidLoad
{
holder = [[NSMutableArray alloc] init];
}
- (IBAction)onSave:(id)sender {
savecount++;
[self saveNumberOfContacts];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:nameTextField.text];
[myArray addObject:phoneTextField.text];
[myArray addObject:addressTextField.text];
[myArray addObject:cityTextField.text];
[myArray addObject:stateTextField.text];
[myArray addObject:zipcodeTextField.text];
[myArray writeToFile:[self saveFilePath] atomically:YES];
[holder addObject:myArray];
}
Have one global array in which you can hold dynamically created array. Now when you get value from holder
, you will have separated myArray
object.
Upvotes: 2