title: How to replace an object in an NSMutableArray at a given index with a new object
body: <p>I have an <code>NSMutableArray</code> object (retained, synthesized as all) that is initiated just fine and I can easily add objects to it using the <code>addObject:</code> method. But if I want to replace an object at a certain index with a new one in that <code>NSMutableArray</code>, it doesn't work.</p>
  
  <p>For example:</p>
  
  <p><code>ClassA.h</code>:</p>
  
  <pre><code>@interface ClassA : NSObject {
      NSMutableArray *list;
  }
  
  @property (nonatomic, copy, readwrite) NSMutableArray *list;
  
  @end
  </code></pre>
  
  <p><code>ClassA.m</code>:</p>
  
  <pre><code>#import "ClassA.h"
  
  @implementation ClassA
  
  @synthesize list;
  
  - (id)init 
  {
      [super init];
      NSMutableArray *localList = [[NSMutableArray alloc] init];
      self.list = localList;
      [localList release];
      //Add initial data
      [list addObject:@"Hello "];
      [list addObject:@"World"];
  }
  
  
  // Custom set accessor to ensure the new list is mutable
  - (void)setList:(NSMutableArray *)newList 
  {
      if (list != newList) 
      {
          [list release];
          list = [newList mutableCopy];
      }
  }
  
  -(void)updateTitle:(NSString *)newTitle:(NSString *)theIndex
  {
      int i = [theIndex intValue]-1;
      [self.list replaceObjectAtIndex:i withObject:newTitle];
      NSLog((NSString *)[self.list objectAtIndex:i]);  // gives the correct output
  }
  </code></pre>
  
  <p>However, the change remains true only inside the method. from any other method, the </p>
  
  <pre><code>NSLog((NSString *)[self.list objectAtIndex:i]);
  </code></pre>
  
  <p>gives the same old value.</p>
  
  <p>How can I actually get the old object replaced with the new one at a specific index so that the change can be noticed from within any other method as well.</p>
  
  <p>I even modified the method like this, but the result is the same:</p>
  
  <pre><code>-(void)updateTitle:(NSString *)newTitle:(NSString *)theIndex
  {
      int i = [theIndex intValue]-1;
  
      NSMutableArray *localList = [[NSMutableArray alloc] init];
      localList = [localList mutableCopy];
      for(int j = 0; j &lt; [list count]; j++)
      {
          if(j == i)
          {
              [localList addObject:newTitle];
              NSLog(@"j == 1");
              NSLog([NSString stringWithFormat:@"%d", j]);
          }
          else
          {
              [localList addObject:(NSString *)[self.list objectAtIndex:j]];
          }
      }
      [self.list release];
      //self.list = [localList mutableCopy];
      [self setList:localList];
      [localList release];
  }
  </code></pre>
  
  <p>Please help out guys :)</p>