Shivbaba's son
Shivbaba's son

Reputation: 1369

Could not "Copy" one mutable array to another

In my iPhone app.

I am copying the one mutable array(with dictionary) to another.

It is like

resultsToDisplay = [[[NSMutableArray alloc]initWithArray:resultsPassed]mutableCopy];


  2012-06-21 17:07:07.441 AllinoneCalc[3344:f803] Results  To Display (
        {
        lbl = "Monthly EMI";
        result = "75.51";
    }
)
2012-06-21 17:07:08.224 AllinoneCalc[3344:f803] Results Passed (
        {
        lbl = "Monthly EMI";
        result = "75.51";
    }
)

Then I am modifying one of them .

[[resultsToDisplay objectAtIndex:i] setValue:[NSString stringWithFormat:@"%.2f",[[[resultsPassed objectAtIndex:i] valueForKey:@"result"] floatValue]] forKey:@"result"];

But what is happening that both are getting edited.

2012-06-21 17:07:08.703 AllinoneCalc[3344:f803] Results Passed (
        {
        lbl = "Monthly EMI";
        result = "75.00";
    }
)
2012-06-21 17:07:08.705 AllinoneCalc[3344:f803] Results  To Display (
        {
        lbl = "Monthly EMI";
        result = "75.00";
    }
)

They both are referring to the same copy.

How to solve this. I want to modify only one array.

Upvotes: 0

Views: 823

Answers (2)

meronix
meronix

Reputation: 6176

an array (or mutable array) is just a list of objects, and you are modifing one of the objects referred in one of your array. the second array is still pointing to the same object, so it's normal that it changes too...

you are copying the array: meaning you're copying the list of objects, not copying all the objects in list.

Upvotes: 1

ekholm
ekholm

Reputation: 2573

You are only copying the array of object pointers, not the object themself. See this article on deep copying.

Upvotes: 1

Related Questions