I have a object from a class that is being copied to a NSMutableArray. I do that 5 times. However, whenever I try to retrieve the objects from that array they are all the same.
Here’s the sample code:
[myProperty setAddress:@"Av. dos Estados, 70."];
[myProperty setPropType:casaRua];
[myProperty setRentalPrice:1430.0];
[propertiesArray addObject:myProperty];
myProperty = [propertiesArray objectAtIndex:0];
NSLog(@"Price 0 %.2f\n", [myProperty rentalPrice]); //In this I got 1430.0
[myProperty setAddress:@"Av. das Nações, 10. Ap: 103."];
[myProperty setPropType:apto];
[myProperty setRentalPrice:450.0];
[propertiesArray addObject:myProperty];
myProperty = [propertiesArray objectAtIndex:0];//indexPath.row];
NSLog(@"Price 0 %.2f\n", [myProperty rentalPrice]); **//In this I got 450.0???**
myP = [propertiesArray objectAtIndex:1];//indexPath.row];
NSLog(@"Price 1 %.2f\n", [myP rentalPrice]); //In this I got 450.0
What am I doing wrong? It seems the array was overwritten with the addObject
You’re re-modifying the same “property” object over and over again.
You need to either
copyit or instantiate a new one each time before modifying it, after adding the previous object.