Saving new input strings to NSMutableArray iPhone

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.

  • How to prevent a globally overridden “new” operator from being linked in from external library
  • Select Multiple Images (UIImagePickerController or Photos.app Share UI)
  • Simple way of using irregular shaped buttons
  • How to speed up the build and run process in unity for mobile devices iOS/Android
  • how to make a keyboard in other language in iOS
  • iOS App crash with SIGTRAP in iPhone 6 plus?
  • 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);
    }
    

    4 Solutions Collect From Internet About “Saving new input strings to NSMutableArray iPhone”

    Because whenever you receive your addChildren action you re-create the childArray which means it will contain 0 objects, then you add one object.

    childArray = [[NSMutableArray alloc] init];
    [self.childArray addObject:inputChildName];
    

    Move childArray = [[NSMutableArray alloc] init]; to an init or load method and it should work.

    Every time you do this:

    childArray = [[NSMutableArray alloc] init];
    

    you create a new NSMutableArray. You have to put this line e.g. in -viewDidLoad to get this code working.

    inputChildArray and childarray are member variables of your class? then you should initialise the array in the init method or smth similar. so just move the line

    // initialise array
    childArray = [[NSMutableArray alloc] init];
    

    into your init method.

    childArray = [[NSMutableArray alloc] init];
    

    You are initializing a new array object every time you save the new string .. ..

    this way you add one string to the array ..and save the new array in place of previous..

    To solve this..add childArray as a member variable to your class.h file..

    and in your viewDidLoad do this

    childArray = [[NSMutableArray alloc] init];
    

    then change your code to below

    -(IBAction)addChildren:(id)sender{
    inputChildName = nameOfChild.text;
    
    
    [self.childArray addObject:inputChildName];
    
    NSUserDefaults *childrenNamesForLabel = [NSUserDefaults standardUserDefaults];
    
    [childrenNamesForLabel setObject:childArray forKey:@"children"];
    
    [childrenNamesForLabel synchronize];
    
     NSLog(@"CHILDREN ARRAY::: %@", childArray);
    
    }