0

I want to know that how can I add NSMutableArray in to an NSMutableArray so that previous data should not lost, and new data will be added on next indexes.

If you don't understand it then you can ask again to me, I will appraise the right answer.

my code is as below

-(void)setArray1:(NSMutableArray *)arrayValueFromNew

{

    self.myArray=arrayValueFromNew;

    myArray2 = [[NSMutableArray alloc] initWithArray:arrayValueFromNew];

    for(int i=0;i<[myArray2 count];i++)
    {
        [myArray addObject:[myArray2 objectAtIndex:i]];
    }

}
1
  • stop adding your code as a comment in every answer. add the code to your question instead Feb 7, 2011 at 11:20

4 Answers 4

0

In your code, myArray and myArray2, both have same objects as you are assigning the arrayValueFromNew array to both. So it kind of doesn't make sense.

But to answer your question 'how to add one array to another?' do :

[mutableArray1 addObjectsFromArray:array2];

EDIT:

this is how your method should look

-(void)setArray1:(NSMutableArray *)arrayValueFromNew

{

    if(!self.myArray)
    {
        self.myArray = arrayValueFromNew;
    }
    else
    {
        [self.myArray addObjectsFromArray:arrayValueFromNew];
    }
}

Your 'myArray must be initialized. You can initialize it in viewDidLoad or init:

self.myArray = [[NSMutableArray alloc] initWithCapacity:1];

12
  • but will it erase the previous data in mutableArray1 or not, cz I dont want to delete the previous array which is saved in mutableArray1, any suggestion? Feb 7, 2011 at 11:48
  • no it will not... thats why the method name is 'add'ObjectsFromArray. Feb 7, 2011 at 11:50
  • not on this time, when next time when another array will come and this method will execute then it is erasing the previous value and just inserting the new value which in coming, try again Feb 7, 2011 at 11:57
  • your code is wrong.. in the very first line you are assigning self.myArray with 'arrayValueFromNew' .. which is the new array... remove that line... Feb 7, 2011 at 12:00
  • is it wrong ? -(void)setArray1:(NSMutableArray *)arrayValueFromNew { self.myArray = arrayValueFromNew; [myArray2 addObjectsFromArray:myArray]; NSLog(@"%@", myArray2); } Feb 7, 2011 at 12:05
0
NSMutableArray *array1 = [NSMutableArray array], *array2 = [NSMutableArray array];

// add some objects to the arrays

[array1 addObjectsFromArray:array2];

//array1 now contains all the objects originally in array1 and array2
4
  • no, it is not working, actual I am passing array from one method to another so logic is quit changed from normal passing values, any idea Feb 7, 2011 at 10:55
  • this is my code, but yet not working, this is method of button, and taking two NSString, saving into an NSMutableArray and then calling method and passing value -(void)setArray1:(NSMutableArray *)arrayValueFromNew { self.myArray=arrayValueFromNew; myArray2 = [[NSMutableArray alloc] initWithArray:arrayValueFromNew]; for(int i=0;i<[myArray2 count];i++) { [myArray addObject:[myArray2 objectAtIndex:i]]; } Feb 7, 2011 at 11:08
  • Please edit the question and make it more clear what you are trying to do. Your code does not make any sense. Feb 7, 2011 at 11:17
  • @Joe BLow and Jakob Egger, Sir I have edited the question, now see it Feb 7, 2011 at 11:22
0

This will work,

NSMutableArray *mutarr=[[NSMutableArray alloc]initWithArray: array1]
5
  • but by doing this, the previous values in *mutaarr will be erased, I want to continue new values in array, and previous values should remain same. Feb 7, 2011 at 10:47
  • for(int i=0;i<[aray1 count];i++) { [array2 addObject:[array1 objectAtIndex:i]]; } Feb 7, 2011 at 10:50
  • this is my code, but yet not working, this is method of button, and taking two NSString, saving into an NSMutableArray and then calling method and passing value -(void)setArray1:(NSMutableArray *)arrayValueFromNew { self.myArray=arrayValueFromNew; myArray2 = [[NSMutableArray alloc] initWithArray:arrayValueFromNew]; for(int i=0;i<[myArray2 count];i++) { [myArray addObject:[myArray2 objectAtIndex:i]]; } Feb 7, 2011 at 11:08
  • please check that you have allocated myArray Feb 7, 2011 at 11:48
  • Even I don't allocate, same problem is there, once I can assign array, but next time it won't work, and replace the value that was previously stored. Feb 7, 2011 at 12:21
0

It looks like you just want a new copy of the old array. There is a handy function for that

NSMutableArray *newArray = [oldArray mutableCopy];

Remember that you've used copy in getting this array so you are responsible for managing the memory of newArray

EDIT

What is your code doing?

-(void)setArray1:(NSMutableArray *)arrayValueFromNew                      //1

{

    self.myArray=arrayValueFromNew;                                       //2

    myArray2 = [[NSMutableArray alloc] initWithArray:arrayValueFromNew];  //3

    for(int i=0;i<[myArray2 count];i++)
    {
        [myArray addObject:[myArray2 objectAtIndex:i]];                   //4
    }

}
  1. This looks like a setter for a property array1
  2. You are setting the property 'array' to arrayValueFromNew. Since I don't know whether this property has been declared with retain or copy I don't know whether array is a pointer to arrayValueFromNew or a pointer to a copy of arrayValueFromNew
  3. You set myArray2 to be a new array that contains the objects of arrayValueFromNew
  4. For each object in myArray2 (which are the objects from arrayValueFromNew. see point 3) you add this object to myArray. Assuming myArray is an NSMutableArray it started with the objects from arrayValueFromNew which you have now added again. It contains each item in arrayValueFromNew twice.
2
  • but when I need it again and again, then what is your suggestion, I don't want to erase old data in my array, so like if 5 indexes are full, then next value should add to 6th index, hope u get what i want Feb 7, 2011 at 12:28
  • Unfortutately, no, I don't understand what you want. You get a copy of the original array and then you can add objects to whatever array you want.
    – Abizern
    Feb 7, 2011 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.