-1

I have an NSMutableArray (detailsMA)having 100 strings like @"True". Am storing that array in NSMutableDictionary which is already allocated.

            NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA];
            [detailsDictionary setObject: array1 forKey:@"detailsMA"];

Now i changed the values of detailsMA, like replacing strings to @"False", After that am retrieving the original array values,

            [detailsMA removeAllObjects];
            [detailsMA addObjectsFromArray:[detailsDictionary objectForKey:@"detailsMA"]];

Here am getting modified values, not the original values. I need original values. Please help me.

Ganee....
  • 302
  • 2
  • 13

1 Answers1

1

You should copy the array.

NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA copyItems:YES];

Don't forget to implement the NSCopying protocol in the classes for the objects to be copied

EDIT:

When you add the objects again, make a copy before adding.

[detailsMA addObjectsFromArray:[[NSArray alloc] initWithArray:[detailsDictionary objectForKey:@"detailsMA"] copyItems:YES]];
Pulkit Goyal
  • 5,604
  • 1
  • 32
  • 50
  • How to implement NSCopying protocol, can you give me example. – Ganee.... Sep 21 '12 at 14:19
  • I thought you were using custom objects. Since you are using NSString, it should be fine anyway. – Pulkit Goyal Sep 21 '12 at 14:20
  • Am using custom object, which is having two properties. With out implementing NSCopying it is crashing – Ganee.... Sep 21 '12 at 14:25
  • You need to respond to the `-copyWithZone:` selector. Have a look at this answer for a description: http://stackoverflow.com/a/4089372/643109 – Pulkit Goyal Sep 21 '12 at 14:28
  • Hi Pulkit Goyal, NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA copyItems:YES]; it is working, but after retrieving from dictionary like [detailsMA removeAllObjects]; [detailsMA addObjectsFromArray:[detailsDictionary objectForKey:@"detailsMA"]]; if change any thing on detailsMA, it is effecting on Dictionary also. I dont want to change Dictionary. – Ganee.... Sep 24 '12 at 08:33
  • Don't add objects, make a copy and then add. See updated answer. – Pulkit Goyal Sep 24 '12 at 14:59