Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace an object in an NSMutableArray at a given index with a new object

I have an NSMutableArray object (retained, synthesized as all) that is initiated just fine and I can easily add objects to it using the addObject: method. But if I want to replace an object at a certain index with a new one in that NSMutableArray, it doesn't work.

For example:

ClassA.h:

@interface ClassA : NSObject {     NSMutableArray *list; }  @property (nonatomic, copy, readwrite) NSMutableArray *list;  @end 

ClassA.m:

#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 } 

However, the change remains true only inside the method. from any other method, the

NSLog((NSString *)[self.list objectAtIndex:i]); 

gives the same old value.

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.

I even modified the method like this, but the result is the same:

-(void)updateTitle:(NSString *)newTitle:(NSString *)theIndex {     int i = [theIndex intValue]-1;      NSMutableArray *localList = [[NSMutableArray alloc] init];     localList = [localList mutableCopy];     for(int j = 0; j < [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]; } 

Please help out guys :)

like image 599
user339076 Avatar asked Jun 17 '10 17:06

user339076


People also ask

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

What is NSMutableArray Objective C?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .


1 Answers

This does the trick:

[myMutableArray replaceObjectAtIndex:index withObject:newObject]; 
like image 112
Mário Carvalho Avatar answered Sep 22 '22 12:09

Mário Carvalho