开发者

how can I save NSMutableArray into NSMutableArray so that previous data in NSMutableArray remain same?

I want to know that 开发者_Python百科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]];
    }

}


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];


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


This will work,

NSMutableArray *mutarr=[[NSMutableArray alloc]initWithArray: array1]


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜