Prashanth Kumar
Prashanth Kumar

Reputation: 7

NSMutable array is not replaced with new object

I have a NSMutableArray which is storing int values like this

[self.destinationPostionPointer addObject:[NSNumber numberWithInt:0]];
[self.destinationPostionPointer addObject:[NSNumber numberWithInt:0]];

after set of code I am trying to replace the above object with

[self.destinationPostionPointer replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:1]];

But the destinationPositionPointer array is not replacing the object with having integer value 1.

Upvotes: 0

Views: 126

Answers (2)

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

You are not allocating memory to this array. So you need to write this line in your code

  - (void)viewDidLoad
    {
         self.destinationPositionPointer = [[NSMutableArray alloc] init];
    }

Upvotes: 3

Nazik
Nazik

Reputation: 8444

if you ant to replace all the values which are having value 0 means

for(int i=0;i< destinationPostionPointer.count;i++)
{
  if([destinationPostionPointer ObjectAtIndex:0 isequaltostring:@"0")
  {
    [self.destinationPostionPointer replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:1]];
  }
} 

Upvotes: -1

Related Questions