Search code examples
iosobjective-cnsmutablearray

How do I update an object in NSMutableArray?


I am trying to update an object in NSMutableArray.

Product *message = (Product*)[notification object];
Product *prod = nil;

for(int i = 0; i < ProductList.count; i++)
{
    prod = [ProductList objectAtIndex:i];
    if([message.ProductNumber isEqualToString:prod.ProductNumber])
    {
        prod.Status = @"NotAvaiable";
        prod.Quantity = 0;
        [ProductList removeObjectAtIndex:i];
        [ProductList insertObject:prod atIndex:i];
        break;
    }
}

Is there any better way to do this?


Solution

  • Remove lines:

    [ProductList removeObjectAtIndex:i];
    [ProductList insertObject:prod atIndex:i];
    

    and that will be ok!