I am using below code to add strings to NSMutableArray. But the issue I am facing is that whenever I add a new value to the NSMutableArray, previous value gets deleted and new value comes in, so there is always a single value.
I have to save this NSMutableArray to NSUserDefaults so that I would use it on other screens too.
I have searched alot please do help me
code is:
- (IBAction)addChildren:(id)sender {
inputChildName = nameOfChild.text;
childArray = [[NSMutableArray alloc] init];
[self.childArray addObject:inputChildName];
NSUserDefaults *childrenNamesForLabel = [NSUserDefaults standardUserDefaults];
[childrenNamesForLabel setObject:childArray forKey:@"children"];
[childrenNamesForLabel synchronize];
NSLog(@"CHILDREN ARRAY::: %@", childArray);
}
Because whenever you receive your addChildren action you re-create the childArray which means it will contain 0 objects, then you add one object.
Move
childArray = [[NSMutableArray alloc] init];to an init or load method and it should work.