Search code examples
iphonensmutablearraynsarray

How to updates array values in iphone app


I have to updates array values at position 2 and 4. How will I update array values. I am initializing my array like below.

ArrayrData=[[NSArray alloc]initWithArray:statuses]; // where statuses is also an array

Solution

  • You can't change the value of NSArray. So initialize your array as Mutable Array

    NSMutableArray *ArrayrData=[[NSMutableArray alloc]init];
    [ArrayrData addObjectsFromArray:statuses];
    

    You can update the value in a NSMutableArray using,

    – replaceObjectAtIndex:withObject:
    – replaceObjectsAtIndexes:withObjects:
    – replaceObjectsInRange:withObjectsFromArray:range:
    – replaceObjectsInRange:withObjectsFromArray: