Search code examples
nsmutablearraynsarray

Array value replacement from array within array in iPhone


I have array output like this.

 {
    id = 1;  
    user = {
            name="ABC"
            }
 },  
{

    id = 2;  
    user = {
            name="XYZ"
            }
 },       

I have to change id with number 5 and 6 and name with "asd" and "fgh". My array definition is here.

      myArray=[[NSMutableArray alloc]initWithArray:statuses];

And here is my approach for chaning of "id".

 [[myArray objectAtIndex:0]replaceObjectAtIndex:0 withObject:@"5"];
 [[myArray objectAtIndex:0]replaceObjectAtIndex:0 withObject:@"6"];

But I am getting following acception.

 [__NSCFDictionary replaceObjectAtIndex:withObject:]: unrecognized selector sent to  instance 0x6511810
 Terminating app due to uncaught    exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x6511810'

Solution

  • myArray seems to be NSArray which is Immutable.

    How did you declare myArray?

    EDIT:

    First:
    

    myArray = [statuses mutableCopy];

    Second:

    [[myArray objectAtIndex:0] setObject:@"5" forKey:@"id"]; 
    [[myArray objectAtIndex:1] setObject:@"6" forKey:@"id"];
    [[[myArray objectAtIndex:0] objectForKey:@"user"] setObject:@"asd" forKey:@"name"];
    [[[myArray objectAtIndex:1] objectForKey:@"user"] setObject:@"fgh" forKey:@"name"];