Im trying to add an object to NSMutableArray it seems to get duplicated.
@interface TestObject : NSObject {
double a_actual;
double a_target;
}
@property(assign) double a_actual;
@property(assign) double a_target;
Create some pointers:
NSMutableArray * myTestObjectArray;
TestObject * myTestObject;
Init them:
myTestObjectArray = [[NSMutableArray alloc] init];
myTestObject = [[TestObject alloc] init];
I add value to the object and add it to the array:
[myTestObject setA_actual:234];
[myJointDataArray insertObject:myTestObject];
I add different values to each object, but i do not necessarily fill all the variables out.
When i print each object out i have the same (last) value duplicated in all objects for some reason.
Printing the array shows that all objects are the same:
Array: (
"<TestObject: 0x6b9b400>",
"<TestObject: 0x6b9b400>",
"<TestObject: 0x6b9b400>",
"<TestObject: 0x6b9b400>",
"<TestObject: 0x6b9b400>",
"<TestObject: 0x6b9b400>" )
Should i alloc a new object of TestObject everytime i want to work with a new?
Yes. If you don’t allocate a new object, you’re just working with the same object over and over again. It appears you want multiple, distinct objects, so allocate a new instance for each one.